Can't access property in returned object
Asked Answered
C

2

0

I've got a problem where I can't seem to query my JSON coming back, I can print out the entire response though, here's my JSON response, I can only see this when I do a msgBox() prompt:

{ "Addresses" : 
    "[{ 
        Building=Megatron Skyscraper,
        BuldingId=1998,
        AccountId=2000,
        Number=007,
        Name=Megatron 
        },{
        Building=StarScream Skyscraper,
        BuldingId=1999,
        AccountId=2001,
        Number=008,
        Name=StarScream
}]"}

And here's my code:

function getReadyStateHandler(req)
{
    // Return an anonymous function that listens to the
    // XMLHttpRequest instance
    return function ()
    {
        // If the request's status is "complete"
        if (req.readyState == 4)
        {
            // Check that a successful server response was received
            if (req.status == 200)
            {
                msgBox("JSON Response recieved...");
                var addresses = req.responseText.toJSON();
                msgBox(req.responseText.toJSON());
            }
            else
            {
                // An HTTP problem has occurred
                alert("HTTP error: " + req.status);
            }
        }
    }
}

I've tried everything from addresses.Address[0].City and addressess.Addresses[0].City and many others - but this is slightly confusing!

Classicist answered 27/5, 2011 at 14:0 Comment(3)
There is no City key in your object. And Adresses is a string for some reason. Even if you use JSON.parse() or eval it will not work as the sting is not valid for parsing. You have to make sure that you get a proper response first.Trap
Hi, looks like everyone is correct, I've been doing: "response.getWriter().write(json.toString());" in my action - Do you know of a better way to handle this? I cant seem to do json.write() as I do not have a write() method for my JSONObject (which is strange!)Classicist
Also meant to state, my actual JSONResponse does contain City, this was just a quickly formed response example.Classicist
L
3

Apart from the fact that there's no City key in your response, your returned object contains only one (malformed) string, not an array of objects. You can check this using http://jsonlint.com

How did you create the response? It should look more like:

{ "Addresses" : [{ 
        "Building":"Megatron Skyscraper",
        "BuldingId":1998,
        "AccountId":2000,
        "Number":7,
        "Name":"Megatron"
        },{
        "Building":"StarScream Skyscraper",
        "BuldingId":1999,
        "AccountId":2001,
        "Number":8,
        "Name":"StarScream"
}]}

Update: those leading zeros in "Number":007 and "Number":008 may cause problems, because they will be interpreted as octal values. I've removed them in my answer.

Lidda answered 27/5, 2011 at 14:6 Comment(4)
Hi, I created the response like so "response.getWriter().write(json.toString());" with json being a JSONObject. I've tried searching for a JSONObject.write() method but my one does not contain such a method - any help is greatly appreciatedClassicist
@majestica: I don't know, ask a new question for that (or look for questions about creating a JSON string in your server side language, it probably has been asked before), as you now know why you can't access your property on the client side. I don't recognize/know that server side language.Lidda
Hi Marcel: heres a link to the server side code that shows how I am building my JSON: #6155345 Appreciate any advice you can giveClassicist
That response you posted is exactly how I want it to look, but cannot seem to get that workingClassicist
D
0

Your response is valid but Addresses is a string and not an array. The quotes shouldn't be there if it is to be treated like an array. You could hack it a bit if you wanted and do

address = JSON.parse(addresses.Addresses);

Driedup answered 27/5, 2011 at 14:8 Comment(2)
Hi John, this is how I am building my JSON response: #6155345 Should I be taking a different approach?Classicist
I recommend using GSON (google's java json library) which can turn any POJO into a JSON-encoded string that the client-side can use w/o any parsing. Once you google GSON there should be plenty of examples to get you going. Hope that helps.Driedup

© 2022 - 2024 — McMap. All rights reserved.