I have a json arry

    var students = {"apResults":[{"offid":"267","item_name":"","offer_name":"fsdfsf","stlongitude":"77.5945627","stlatitude":"12.9715987"},

{"offid":"265","item_name":"","offer_name":"vess offer shops","stlongitude":"","stlatitude":""},

{"offid":"264","item_name":"","offer_name":"vess ofer  shop","stlongitude":"","stlatitude":""},
{"offid":"263","item_name":"","offer_name":"ofer frm vess","stlongitude":"77.5943760","stlatitude":"12.9716060"},
{"offid":"262","item_name":"","offer_name":"offer hungamma","stlongitude":"77.5943760","stlatitude":"12.9716060"},
{"offid":"261","item_name":"","offer_name":"offer hungamma","stlongitude":"77.5943760","stlatitude":"12.9716060"},
{"offid":"260","item_name":"","offer_name":"offer1","stlongitude":"77.5943760","stlatitude":"12.9716060"},
{"offid":"259","item_name":"","offer_name":"offer","stlongitude":"77.5943760","stlatitude":"12.9716060"}]}

How i can parse this json arry using json.parse. I have tried this code

    for(i=0;i<students.apResults.length;i++)
{
    var contact = JSON.parse(students.apResults);
    var offid = contact.offid;
    alert(offid)    
}

But its giving an error JSON.parse: unexpected character.Edited my question

share|improve this question

73% accept rate
What's the error? Which browser? – danwellman Mar 14 at 11:47
2  
What is the use of parsing it. it is already a javascript object use students.Maths[i].Name to access name of each student – Nemoy Mar 14 at 11:49
@danwellman - I think you're after quick upvotes here for requesting more information when in fact more information was not required had you have just read the post. Despite the fact it's considered good practice to also provide more details of the error (and often crucial) I don't think it was here. – SkonJeet Mar 14 at 11:52
@Skonjeet I was asking for more info simple because saying 'I have an error' and then not stating what the error is is like walking into a shop and saying 'I want something' and then stading still and waiting. I am not looking for quick upvotes. Do you even get rep from comment upvotes? – danwellman Mar 14 at 12:07
1  
@SkonJeet. I deleted the comment that you upvoted, not because I don't stand by what I said but because I do not need or want any form of rep from you. – danwellman Mar 20 at 17:52
show 5 more comments
feedback

7 Answers

That's not a json string, that's a regular javascript variable:

for(i=0;i<students.Maths.length;i++)
{
    var contact = students.Maths[i];
    var fullname = contact.Name;
    alert(fullname) 
}
share|improve this answer
if i remove Maths and Science is it a json string? – Warrior Mar 14 at 11:50
It's still an object. Here the JSON string representation: alert(JSON.stringify(students)) – ZER0 Mar 14 at 11:52
1  
@THOmas - you need to read more on JSON mate. Think of typical JSON as a big string that can then be interpreted (parsed) by javascript into an actual javascript object. What you have is already the object, you'd be taking a huge step back if you were to want to deal with JSON from an object you already have in order to parse it back as an object. – SkonJeet Mar 14 at 11:54
@THOmas - there is on json in your code snippets at all. – cps7 Mar 14 at 12:45
feedback

students is not a JSON array, it's an actual array. You don't have to parse because it's not a string. So you can access directly to the data you need:

for(i=0;i<students.Maths.length;i++) {
    var contact = students.Maths[i];
    var fullname = contact.Name;
    alert(fullname) 
}
share|improve this answer
feedback

What you have is a javascript object. So, you won't need the JSON.parse

for(i=0;i<students.Maths.length;i++)
{
    var contact = students.Maths[i]);
    var fullname = contact.Name;
    alert(fullname) 
}

this should be ok

share|improve this answer
feedback

The idea of JSON is for the exchange of objects represented as a structured string (in a nutshell). What you've got there is simply an object. It's unnecessary (and impossible) to parse and object that isn't JSON into a javascript object; what you have is the outcome of what you would expect from a parsed JSON string.

share|improve this answer
feedback

JSON parses strings, not objects/arrays.

why need parsing when you can access it like students.Maths[i].Name

share|improve this answer
feedback

You can't parse students because is not a JSON. It's simple object.

However this will work:

var students = JSON.stringify(students); // if you want to send data

students = JSON.parse(students); // after receiving make a object from it

//use like any object    
for(i=0;i<students.Maths.length;i++)
{
    var contact = students.Maths[i];
    var fullname = contact.Name;
    alert(fullname) 
}

Of course it doesn't make sense to write it that way unless you send students data to other site or program.

Edit: You don't need JSON in this code at all. But if you want to test JSON.parse() do it this way:

var students = { ... } // your data

var students = JSON.stringify(students); // students is `object`, make it `string`

students = JSON.parse(students); // now you can parse it, `students` is object again 

for(i=0;i<students.apResults.length;i++) {
    var contact = students.apResults; // no JSON
    var offid = contact.offid;
    alert(offid)    
}

That should work.

share|improve this answer
I have edited my question... – Warrior Mar 14 at 12:23
feedback
up vote 0 down vote accepted
for(i=0;i<students.apResults.length;i++)
{

    var contact = JSON.parse(students.apResults[i].offid);

    alert(contact)  
}
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.