How to decode a JSON string?

后端 未结 4 614
名媛妹妹
名媛妹妹 2021-01-21 10:39

On the server side do I have 2 hashes I encode into JSON strings like so

my $j = JSON->new;
$j = $j->utf8;

my $data;
$data->{users}  = $j->encode(\\         


        
相关标签:
4条回答
  • 2021-01-21 11:14

    You can use the JSON parser created by douglas crockford:
    https://github.com/douglascrockford/JSON-js

    include the json2.js in your page, the you can do:

    var object = JSON.parse(string);
    

    Then you can use it as an array.

    0 讨论(0)
  • You should use jQuery.getJSON() as mentioned at http://api.jquery.com/jQuery.getJSON/.

    There is also $.parseJSON() method to parse string to json if you want to go that way.

    0 讨论(0)
  • 2021-01-21 11:22

    you can use for in statement

    var index = 0;
    for(user in users){
      result += '<tr><td>' + index + '</td><td>' + user['fn'] + '</td></tr>\n';
    index++;
    }
    
    0 讨论(0)
  • 2021-01-21 11:25

    You don't need to turn it into an array. According to the jQuery.each() documentation it takes both arrays or objects and JSON is a subset of the object literal notation of JavaScript.

    Edit: Here's an example: http://jsfiddle.net/pedrocorreia/s5UrZ/2/

    0 讨论(0)
提交回复
热议问题