如何使用jQuery读取多级Json数据? [英] How to read multi-level Json data by using jQuery?

查看:351
本文介绍了如何使用jQuery读取多级Json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Json数据:

Here is my Json data:

[{"ok" : false,
  "details" : [{"n" : "Email",
                "v" : "The email address you entered is not correct."},
               {"n" : "NameFull",
                "v" : "Please enter your full name."}]
 }]

我发现读取单信号级别的Json数据非常容易,但是对于像上面这样的多级别数据,我无法通过搜索互联网找到答案.我只想阅读以上值.就像从详细信息"下的"n"和"v"中读取值一样.

I found read sigle-level Json data is super-easy but for multi-level data like above I couldn't find an answer by search the internet. I just want to read above values. Like reading value from "n" and "v" under the "details".

推荐答案

数据在数组内部

请注意数据周围的方括号.您将必须索引上述数据集中的数组.

Note the square brackets around your data. You will have to index into the array in the above set of data.

这应该使您可以访问电子邮件值:

This should let you access the email value:

var json = '[{"ok":false,"details":[{"n":"Email","v":"The email address you entered is not correct."},{"n":"NameFull","v":"Please enter your full name."}]}]';

var k = eval( json );

alert( k[0].details[0].v );

这是出现的警报文本:

The email address you entered is not correct.

也许您的JSON源将其放入数组中,因为他们想返回多个元素?

Maybe your JSON source put it inside an array because they want to return multiple elements?

实际上,您不需要jQuery. JSON代表JavaScript Object Notation.这基本上意味着它是一个Javascript代码块,可以直接在JavaScript中按原样对其进行评估以获取序列化数据,在您的情况下,该数据恰好是数组.

And actually, you don't need jQuery for this. JSON stands for JavaScript Object Notation. That basically means that it is a Javascript block of code that can be evaluated as-is directly in JavaScript to get the serialized data, which, in your case happens to be an array.

这篇关于如何使用jQuery读取多级Json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆