How could I parse through this JSON object in JQuery?
本问题已经有最佳答案,请猛点这里访问。
我有一个JSON对象,它没有给定三个值的键(每个值都是一个数组),我想通过它们进行解析。如何在jquery中执行此操作?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | [ { "cid":"3", "pid":"0", "nid":"12", "uid":"4", "subject":"test2", "hostname":"127.0.0.1", "created":"1374084646", "changed":"1374084645", "status":"1", "thread":"02/", "name":"chrisr", "mail":"", "homepage":"", "language":"en", "uuid":"e4729a69-7f6f-4091-98a0-0a040fe683f1", }, { "cid":"2", "pid":"0", "nid":"13", "uid":"4", "subject":"TEST comment 2", "hostname":"127.0.0.1", "created":"1374072245", "changed":"1374072244", "status":"1", "thread":"01/", "name":"chrisr", "mail":"", "homepage":"", "language":"en", "uuid":"b4d5a084-8aa3-4828-b6e4-17396cbaf2f6", }, { "cid":"1", "pid":"0", "nid":"12", "uid":"4", "subject":"test comment", "hostname":"127.0.0.1", "created":"1374072176", "changed":"1374072175", "status":"1", "thread":"01/", "name":"chrisr", "mail":"", "homepage":"", "language":"en", "uuid":"7ade4906-7d6e-4cad-9f97-7f43eadea731", } ] |
您的JSON无效。
一旦创建了一个有效的JSON字符串,解析它就非常简单。
使用以下步骤:
这是一把工作的小提琴。
它的功能如下:
1 2 | var jsonText = '[ {"cid":"3","pid":"0","nid"...} ]'; var jo = $.parseJSON(jsonText); |
如果JSON是字符串形式的,那么可以使用
现代浏览器本身就具有此功能(不需要jquery),但您也可以从以下位置之一将其包含在内:
- 网址:https://github.com/douglascorkford/json-js
- https://code.google.com/p/json-sans-eval/
有关更完整的列表,请参见json.org。