[英]Parse json array using javascript
I have a json arry 我有一个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
. 我如何使用json.parse
解析这个json 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 但它给出了一个错误JSON.parse: unexpected character
编辑我的问题
That's not a json string, that's a regular javascript variable: 这不是一个json字符串,这是一个常规的javascript变量:
for(i=0;i<students.Maths.length;i++)
{
var contact = students.Maths[i];
var fullname = contact.Name;
alert(fullname)
}
for(i=0;i<students.apResults.length;i++)
{
var contact = JSON.parse(students.apResults[i].offid);
alert(contact)
}
JSON parses strings, not objects/arrays. JSON解析字符串,而不是对象/数组。
why need parsing when you can access it like students.Maths[i].Name
为什么需要解析,当你可以像students.Maths[i].Name
一样访问它students.Maths[i].Name
students
is not a JSON array, it's an actual array. students
不是JSON数组,它是一个实际的数组。 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)
}
You can't parse students
because is not a JSON. 您无法解析students
因为它不是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. 当然,除非您将students
数据发送到其他网站或程序,否则以这种方式编写是没有意义的。
Edit: You don't need JSON in this code at all. 编辑:您根本不需要此代码中的JSON。 But if you want to test JSON.parse() do it this way: 但是如果你想测试JSON.parse()就这样做:
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. 这应该工作。
What you have is a javascript object. 你有一个javascript对象。 So, you won't need the JSON.parse 所以,你不需要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 这应该没问题
The idea of JSON is for the exchange of objects represented as a structured string (in a nutshell). JSON的想法是用于交换表示为结构化字符串的对象(简而言之)。 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; 将不是JSON的对象和对象解析为javascript对象是不必要的(也是不可能的); what you have is the outcome of what you would expect from a parsed JSON string. 你所拥有的是你对解析的JSON字符串的期望结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.