0

I am running a Javascript function on the latest version of Mozilla which receives a string which I want to convert to a JSON object. The conversion seems to be failing.

The string is being generated on the server side in a Java function:

         result = "[{ \"userID\": 1 \"firstName\":\"John\" \"lastName\":\"Sheridan\" }{ \"userID\": 2 \"firstName\":\"Michael\" \"lastName\":\"Geribaldi\" }]";

(note that I am attempting to return an array of values for a list).

The code on the client side is the ajax callback shown below:

var successFunc = function(data, textStatus, jqXHR)
{
    alert("Data: "+data);

    var obj = $.parseJSON(data);
    alert("Object: "+obj);
}

Apparently, the data is coming back to the callback and is being displayed in its string form, but the JSON parser is failing because the second alert is failing to appear. I am sure something is wrong with my string but am having trouble figuring out what. The debugger is not telling me anything, I am just seeing a silent failure.

I have also attempted this using the JSON.parser() function. I am seeing the same thing. I am making a mistake somewhere. Can someone tell me where?

1
  • Your string is not in proper format.... missing , after \"userID\": 1 and \"firstName\":\"John\" Commented Aug 21, 2015 at 3:20

2 Answers 2

2

Your json is not valid, you are missing comma

In order to parse your json should be like this

[
 { "userID": 1, "firstName":"John", "lastName":"Sheridan" },
 { "userID": 2, "firstName":"Michael", "lastName":"Geribaldi" }
]
Sign up to request clarification or add additional context in comments.

Comments

1

JSON is a format where data is in key:value pairs separeted by , and key and value are enclosed in double quotes, where objects are enclosed in {} braces and array is enclosed in [] hope you have got the your mistake that where your json is lacking.

  "[{ 
    \"userID\": 1 ,
    \"firstName\":\"John\",
    \"lastName\":\"Sheridan\",
     },
    { 
    \"userID\": 2 ,
    \"firstName\":\"Michael\",
    \"lastName\":\"Geribaldi\" }]"

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.