2

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(\%user_result);
$data->{owners} = $j->encode(\%owner_result);
$json_string    = to_json($data);

print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_string;

On the client side I have

$(document).ready(function(){

    $('form').live('submit', function(){

    $.ajax({
        type: "GET",
        url: "/cgi-bin/ajax_confirm.pl",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        data: $(this).serialize(),

        error: function(XMLHttpRequest, textStatus, errorThrown) { 
        $('div#create_result').text("responseText: " + XMLHttpRequest.responseText +
                        ", textStatus: " + textStatus +
                        ", errorThrown: " + errorThrown);
        $('div#create_result').addClass("error");
        },

        success: function(result){
        if (result.error) {
            $('div#create_result').text("result.error: " + result.error);
            $('div#create_result').addClass("error");
        } else { // perl script says everything is okay

            var users  = result.users;
            var owners = result.owners;
...

users contains

{"ss":"Sandra Schlichting","fn":"Full name"}

but it is not an array. When I use $.each() it takes on character at a time.

Problem

How do I turn it into an array, so I can use

function makeTable(users) {
    var result = '<table>\n<tr><td>Initials</td><td>Full Name</td></tr>\n';
    $.each(users, function(index, value) {
        result += '<tr><td>' + index + '</td><td>' + value + '</td></tr>\n';
    });
    result += '</table>';
    return (result);
}

which should produce

Initials    Full Name
ss  Sandra Schlichting
fn  Full name
3
  • You should leave a comment for the person who wrote that jQuery code in your last question. Not open a new question, it just causes bad fragmentation. Commented May 17, 2011 at 12:01
  • possible duplicate of How to include array content in HTML? Commented May 17, 2011 at 12:01
  • @Gary Green : I thought about that, but he solved the question to perfection, so I figured it would be off-topic? Commented May 17, 2011 at 12:13

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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/

2 Comments

Why does this work? What does var res = makeTable(my); \n document.write(res); do?
it declares a variable and assigns the return value of your makeTable function and then writes it to the html document. the variable 'my' supplied to the function contains the json object you decribed.
1

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.

1 Comment

You could also use the build-in JSON parser: $.parseJSON(data)
0

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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.