Json array parameter using javascript

I have 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 can I parse this json arry using json.parse . I tried this code

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

But this gives a JSON.parse: unexpected character error. Changed my question

+4
source share
7 answers
 for(i=0;i<students.apResults.length;i++) { var contact = JSON.parse(students.apResults[i].offid); alert(contact) } 
+4
source

This is not a json string, which is a regular javascript variable:

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

JSON parses strings, not objects / arrays.

why do you need parsing when you can access it, for example students.Maths[i].Name

+3
source

students not a JSON array, it is an actual array. You do not need to understand, because this is not a string. Thus, you can access the data you need:

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

You cannot parse students because it is not JSON. This is a 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 makes no sense to write this way if you do not send students data to another site or program.

Edit: You do not need JSON at all in this code. But if you want to check JSON.parse (), do it like this:

 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.

+2
source

You have a javascript object. This way you will not need JSON.parse

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

it should be ok

+1
source

The idea of ​​JSON is to exchange objects represented as a structured string (in a nutshell). What you have is just an object. There is no need (and impossible) to parse and create non-JSON objects into a javascript object; what you have is the result of what you expect from a JSON syntax string.

0
source

Source: https://habr.com/ru/post/1401358/


All Articles