最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing JSON under Array object - Stack Overflow

programmeradmin2浏览0评论

I have a simple JSON string, encapsulated in an array created using JSONArray and JSONObject form org.json in Java.

var outObjA = [{"LoginTime":"2018-02-14 08:51:48.0","User":"f00dl3","RemoteIP":"127.0.0.1"}];

I am trying to parse this in JavaScript. First I iterate over the array encapsulating the data using an `i" counter:

for(var i = 0; i < outObjA.length; i++) {
   var jsonData = JSON.parse(outObjA[i]);
   console.log(jsonData);
}

When I attempt to parse the JSON, I get an error:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

I built a try/catch into it and it returns an object:

for (var i = 0; i < outObjA.length; i++) {
  var jsonData = null;
  try {
    jsonData = JSON.parse(outObjA[i]);
  } catch (e) {
    jsonData = outObjA[i];
  }
  console.log(jsonData);
}

Returned:

{
  "LoginTime": "2018-02-14 08:51:48.0",
  "User": "f00dl3",
  "RemoteIP": "127.0.0.1"
}

This is valid JSON, is it not?

I have a simple JSON string, encapsulated in an array created using JSONArray and JSONObject form org.json in Java.

var outObjA = [{"LoginTime":"2018-02-14 08:51:48.0","User":"f00dl3","RemoteIP":"127.0.0.1"}];

I am trying to parse this in JavaScript. First I iterate over the array encapsulating the data using an `i" counter:

for(var i = 0; i < outObjA.length; i++) {
   var jsonData = JSON.parse(outObjA[i]);
   console.log(jsonData);
}

When I attempt to parse the JSON, I get an error:

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data

I built a try/catch into it and it returns an object:

for (var i = 0; i < outObjA.length; i++) {
  var jsonData = null;
  try {
    jsonData = JSON.parse(outObjA[i]);
  } catch (e) {
    jsonData = outObjA[i];
  }
  console.log(jsonData);
}

Returned:

{
  "LoginTime": "2018-02-14 08:51:48.0",
  "User": "f00dl3",
  "RemoteIP": "127.0.0.1"
}

This is valid JSON, is it not?

Share Improve this question edited Feb 14, 2018 at 16:03 Racil Hilan 25.4k13 gold badges56 silver badges61 bronze badges asked Feb 14, 2018 at 15:40 user3260912user3260912 6412 gold badges7 silver badges19 bronze badges 7
  • Thats not json... that's javascript object, you can't parse a JavaScript object, remove JSON.parse( – Liam Commented Feb 14, 2018 at 15:41
  • 2 outObjA is already an object you don't have to parse it! If it was a string you'd have to. – Liora Haydont Commented Feb 14, 2018 at 15:42
  • stackoverflow./questions/8294088/javascript-object-vs-json – messerbill Commented Feb 14, 2018 at 15:43
  • it's an object written in JavaScript Object Notation, but since it's already in Javascript it doesn't need to be parsed. – I wrestled a bear once. Commented Feb 14, 2018 at 15:44
  • @Occam'sRazor JSON cannot be an object, so to say it's an object written .... is confusing. JSON is a string (which I suppose is an object...), that represents an object – Liam Commented Feb 14, 2018 at 15:48
 |  Show 2 more ments

3 Answers 3

Reset to default 4

That's not a JSON string, it's a JavaScript array. To make it a JSON string, surround it with apostrophes, then you can parse it, and finally loop through it:

var outObjA = '[{"LoginTime":"2018-02-14 08:51:48.0","User":"f00dl3","RemoteIP":"127.0.0.1"}]';

var outObjA = JSON.parse(outObjA);
for (var i = 0; i < outObjA.length; i++) {
  var jsonData = outObjA[i];
  console.log(jsonData);
}

Or better, you can loop through it directly without parsing:

var outObjA = [{"LoginTime": "2018-02-14 08:51:48.0", "User": "f00dl3", "RemoteIP": "127.0.0.1"}];

for (var i = 0; i < outObjA.length; i++) {
  var jsonData = outObjA[i];
  console.log(jsonData);
}

This line is not necessary.

 for(var i = 0; i < outObjA.length; i++) {
    var jsonData = JSON.parse(outObjA[i]);
    console.log(jsonData);
}

Because outObjA is a array type not json,it does not need parsing simply retrieve it and display it.

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned, source.

To expand further take this example from Mozilla ,

var json = '{"result":true, "count":42}';

The reason why this needs parsing is because its a string type, the JSON.parse converts that string into a JSON object, thus making it accessible. like this,

obj = JSON.parse(json);

console.log(obj.count);
// expected output: 42

console.log(obj.result);
// expected output: tru

The only way your code would work is if you did this.

var outObjA = ['{"LoginTime":"2018-02-14 08:51:48.0","User":"f00dl3","RemoteIP":"127.0.0.1"}']; 

This way the element at position zero is a string, not a JSON object.

To conclude, strings are not JSON.

  1. JSON objects are surrounded by curly braces {}.
  2. JSON objects are written in key/value pairs.
  3. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
  4. Keys and values are separated by a colon.
  5. Each key/value pair is separated by a ma.

you do not need parse for it is already json

you might use instead

var jsonData = outObjA[i];
console.log(jsonData['User']); // or any other key
发布评论

评论列表(0)

  1. 暂无评论