2

When I make an ajax call I am getting response as multiple json objects.

//here is the response when I make ajax call:
{"name":"suresh","class":"10th"},{"name":"suresh","class":"10th"}

want to put this response into array.

Could any one help me how to split the objects and put into array?

2
  • 3
    If the response you are getting is exactly how you posted it's not valid JSON. What's the exact JSON you're getting? If it's enclosed in [ and ] then it's already an array. Commented Nov 30, 2011 at 7:50
  • post code pertaining to your ajax call, and what exactly you want captured in the array (values only?) and if you will be looping several objects, or looping the whole ajax call etc... Commented Nov 30, 2011 at 7:51

2 Answers 2

1

Can you change the JSON response that you're getting, or is that out of your control? That response is not valid JSON. It needs to be wrapped in square brackets to turn it into an array:

[{"name":"suresh","class":"10th"},{"name":"suresh","class":"10th"}]
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the response Jan,yes i am getting response as like i mentioned above.if i add [] braces to my response in java can i use it as array.in that case i need to hard code the square braces in the backend java code.
Then I'd say your best bet is to wrap the response string in square brackets before parsing it (@bazmegakapa gives more details in his response).
0

The response seems to be quite broken (if you have any chance to change it or have it changed then follow @tomtheman5's advice). If you expect it to always be like this in the future, you can use this dirty trick:

var stuff = JSON.parse('[' + jsonResponse + ']');

Use with caution though.

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.