How To Parse JSON With Number As A Key
I have the following json, I don't have any control over this output unfortunately. { 'questions': { '9733': { 'text': 'Star Trek or Star Wars?', 'answers': {
Solution 1:
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 not work.
Solution 2:
Solution 3:
I believe you can access the data via same syntax as in an array:
console.log(jsonData.questions[9733].text);
Solution 4:
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.
Solution 5:
Try using Ason, If you are using Java 8. Gradle dependency compile 'org.arivu:ason:1.0.3'.
Java code as follows
Ason ason = new Ason();
Object json = ason.fromJson("<<JSON String!>>");
System.out.println(Ason.getJson(json, "questions.9733.text", null)):
Post a Comment for "How To Parse JSON With Number As A Key"