0

I am trying to create a JSON object from a string in the correct JSON format I received from my .jsp file. I get the string and convert it as follows:

try{
        var jsonStringer = JSON.stringify(mazeFromServer);
        var obj = JSON.parse(jsonStringer);
    }catch (e){
        window.alert(e);
    }

This works without errors. When I try to manipulate or get information from that object, for instance:

var stringName = obj.Name;

Although i have a Name field in my JSON, nothing happens. I checked my JSON on the JSON validation website and everything was fine. What is wrong?

JSON looks like this:

{
        "Name": "Game1",
        "Level":"Two"};

When I declare JSON by myself, it works fine. But when I receive it in a String format from an outside source it doesn't work.

Any Ideas?

EDIT:

mazeFromServer is a string received from an external server. In my servlet i am adding it as follows:

JSONObject obj = new JSONObject();
    obj.put("progress", fromServer);

And then running this Query in my jsp file:

function getMaze(){
    $.getJSON("ProgressServlet", function(data){
        if (data.progress != current)
                mazeFromServer = data.progress;
            $('.mazeLabel').text(mazeFromServer);
        stopJSONCheck();
    })
}

The mazeFromServer Looks as follows:

{

"Name": "Game1", "Maze": "111111111110000000", "Start": { "Row": 3, "Col": 3 }, "End": { "Row": 1, "Col": 3 } }

35
  • 1
    please replace JSON object with object. JSON is always a string. Commented May 25, 2016 at 14:03
  • 1
    What is in mazeFromServer and why do you stringify it? Commented May 25, 2016 at 14:05
  • 1
    You're stringifying something which is already a string, then unstringifying the results. Skip the first step. Commented May 25, 2016 at 14:10
  • 2
    Yeah, get rid of var jsonStringer = JSON.stringify(mazeFromServer); and change the obj line to var obj = JSON.parse(mazeFromServer); Commented May 25, 2016 at 14:11
  • 1
    This question is tagged as jQuery (though no jQuery code is shown). That library will normally convert from/to JSON automatically in many circumstances so you'll probably get better answers if you provide some context. Commented May 25, 2016 at 14:12

1 Answer 1

1

You should modify your code as follows:

    try{
        var obj;

        if ( typeof mazeFromServer != "string" ) {
            obj = jsonStringer;
        } else {
            var jsonStringer = JSON.stringify(mazeFromServer);

            if ( typeof jsonStringer != "string" ) {
                throw("mazeFromServer cannot be converted to JSON string");
            }   
            obj = JSON.parse(jsonStringer);
        }
        if ( typeof obj != "object" ) {
            throw("'obj' is not an object, it is: " + typeof obj);
        }
    } catch (e) {
        window.alert(e);
    }
Sign up to request clarification or add additional context in comments.

14 Comments

It throws: "Cannot JSON string back to object"
Can you post the content of mazeFromServer plz? Try the insertion I posted above before calling stringify.
Throws: "mazeFromServer is not an object"
Modified again, might give you some clue.
Throws: mazeFromServer is not an object, it is: string
|

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.