我如何在 JQuery 中解析这个 JSON 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17707748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:47:31  来源:igfitidea点击:

How could I parse through this JSON object in JQuery?

jqueryjsonparsing

提问by CR47

I have a JSON object that doesn't have a key for the three values given (each is an array) and I want to parse through them. How might I do this in JQuery?

我有一个 JSON 对象,它没有给定三个值的键(每个值都是一个数组),我想解析它们。我如何在 JQuery 中做到这一点?

[
    {
        "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",
    }
]

采纳答案by jahroy

Your JSON is not valid.

您的 JSON 无效。

Once you create a valid JSON string, parsing it is verysimple.

一旦创建了有效的 JSON 字符串,解析它就非常简单。

Use the following steps:

使用以下步骤:

  1. remove the commas after the last property of each object
  2. remove the newlines
  3. wrap the JSON text in single quotes
  4. call jQuery.parseJSON()on the text
  1. 删除每个对象的最后一个属性后的逗号
  2. 删除换行符
  3. 将 JSON 文本括在单引号中
  4. 呼唤jQuery.parseJSON()文本

Here's a working fiddle.

这是一个工作小提琴

It does something like this:

它做这样的事情:

var jsonText = '[ { "cid": "3", "pid": "0", "nid"...} ]';
var jo = $.parseJSON(jsonText);

回答by FishBasketGordo

If you have the JSON in string form, you can use JSON.parse[MDN]to get it in object form, and then do with it what you need to.

如果你有字符串形式的 JSON,你可以使用JSON.parse[MDN]以对象形式获取它,然后用它做你需要的。

Modern browsers have this function natively - no jQuery necessary - but you can also include it yourself from one of these locations:

现代浏览器本机具有此功能 - 不需要 jQuery - 但您也可以从以下位置之一包含它:

For a more complete list, see JSON.org.

有关更完整的列表,请参阅JSON.org