javascript 如何以数字为键解析JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23552708/
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-10-28 01:04:55  来源:igfitidea点击:

How to parse JSON with number as a key

javascriptjson

提问by Caimen

I have the following json, I don't have any control over this output unfortunately.

我有以下 json,不幸的是我无法控制这个输出。

{
"questions": {
    "9733": {
        "text": "Star Trek or Star Wars?",
        "answers": {
            "41003": "Star Trek",
            "41004": "Star Wars",
            "41005": "Neither is superior in my opinion; both great in their own ways",
            "41006": "Not a fan",
            "41007": "I don't have an opinion on this"
        }
    },
    "25272": {
        "text": "Which of these summer movies are you looking forward to the most?",
        "answers": {
            "99545": "World War Z",
            "99546": "Monsters University ",
            "99547": "White House Down",
            "99548": "Man of Steel",
            "99549": "Lone Ranger",
            "99550": "The Wolverine"
        }
    },
    "27547": {
        "text": "Should the U.S. attack Syria?",
        "answers": {
            "107453": "Yes",
            "107454": "No"
        }
    }
}
}

I am using json.parse to parse this. To get the text of the first question I would normally do something like this.

我正在使用 json.parse 来解析这个。为了获得第一个问题的文本,我通常会做这样的事情。

var jsonData = JSON.parse(data);//Where data = the json above
console.log(jsonData.questions.9733.text);//Obviously this fails

However javascript doesn't like that number in there obviously. How would you recommend accessing the text of the first question? I would prefer the json to be setup better with in an array of questions instead. Unfortunately I don't have any control over the output of this JSON.

然而,javascript 显然不喜欢那里的那个数字。您建议如何访问第一个问题的文本?我更喜欢在一系列问题中更好地设置 json。不幸的是,我无法控制这个 JSON 的输出。

I'm also not going to be aware of the what the keys are as they come across, but thats a whole other issue. I'm willing entertain any suggestions on how to parse this thing as I've never had to parse such a strange JSON output.

我也不会意识到他们遇到的键是什么,但那是另一个问题。我愿意接受关于如何解析这个东西的任何建议,因为我从来没有解析过这样一个奇怪的 JSON 输出。

回答by p.s.w.g

You need to use bracket notation:

您需要使用括号表示法

console.log(jsonData.questions["9733"].text);

But because the value inside the brackets will be automatically converted a string, this would also work:

但是因为括号内的值会自动转换为字符串,所以这也可以:

console.log(jsonData.questions[9733].text);

Note however, that using non-strings is as property names generally bad form and could lead to some subtle problems, e.g. if the property name was "001", then [001]would notwork.

但是请注意,使用非字符串作为属性名称一般状态不佳,并可能导致一些微妙的问题,例如,如果属性名称是"001",那么[001]工作。

回答by Tohveli

I believe you can access the data via same syntax as in an array:

我相信您可以通过与数组相同的语法访问数据:

console.log(jsonData.questions[9733].text);

回答by VictorR

Why don't you try?

你为什么不试试?

jsonData["questions"]["9733"]

How to access a numeric property?

如何访问数字属性?

回答by Kyle Paulsen

If you have to use numbers as keys... you can access them like this:

如果您必须使用数字作为键……您可以像这样访问它们:

var text = jsonData.questions["9733"].text;

Edit: You can also access it with the number 9733. It doesn't have to be a string. Only needs to be a string if the key is non-numeric.

编辑:您也可以使用号码 9733 访问它。它不必是字符串。如果键是非数字的,则只需要是一个字符串。

回答by parthipang

Try using Ason, If you are using Java 8. Gradle dependency compile 'org.arivu:ason:1.0.3'.

尝试使用 Ason,如果您使用的是 Java 8。Gradle 依赖编译 'org.arivu:ason:1.0.3'。

Java code as follows

Java代码如下

Ason ason = new Ason();
Object json = ason.fromJson("<<JSON String!>>");
System.out.println(Ason.getJson(json, "questions.9733.text", null)):