简体   繁体   English

JSON 类 JSON 对象 javascript

[英]JSON inside JSON-like object javascript

I have generated a json-type variable:我生成了一个 json 类型的变量:

var jsonLines = [[{"x":"275","y":"231"},{"x":"124","y":"237"}],[{"x":"201","y":"157"},{"x":"275","y":"231"}],[{"x":"215","y":"307"},{"x":"201","y":"157"}],[{"x":"342","y":"188"},{"x":"215","y":"307"}]];

I want to parse this JSON-like object and print the corresponding entities.我想解析这个类似 JSON 的对象并打印相应的实体。 I tried many solutions of similar problems here at SO, but nothing worked on this ( for each loop, by indexing etc.).我在 SO 上尝试了许多类似问题的解决方案,但没有任何效果(对于每个循环,通过索引等)。 It'll great if anyone could help me out.如果有人可以帮助我,那就太好了。 Thank you.谢谢你。

JSON is short for JavaScript Object Notation. JSON 是 JavaScript Object Notation 的缩写。 What you have there is a Javascript object literal , and it can be treated as such.你有一个 Javascript object literal ,它可以被这样对待。

for(var i = 0; i < jsonLines.length; i++){
    var innerArray = jsonLines[i];

    for(var j = 0; j < innerArray.length; j++){
        var obj = innerArray[j];

        //now you can use obj.x, obj.y etc...
    }
}

JSON is heavily based off JavaScript object literals so when it is in actual code and not in a string/text file, it is actually a JavaScript object. JSON 在很大程度上基于 JavaScript 对象文字,因此当它在实际代码中而不是在字符串/文本文件中时,它实际上是一个 JavaScript 对象。

You can break-down the object like so你可以像这样分解对象

//An Array of...
[
    //Arrays of
    [
        //JavaScript Object Literals
        {"x":"275","y":"231"},
        {"x":"124","y":"237"}
    ],
    [{"x":"201","y":"157"},{"x":"275","y":"231"}],
    [{"x":"215","y":"307"},{"x":"201","y":"157"}],
    [{"x":"342","y":"188"},{"x":"215","y":"307"}]
]

Also worth nothing that JavaScript Object property names can be strings JavaScript 对象属性名称可以是字符串也毫无价值

var obj1 = {
  a : "someValue"
}

var obj2 = {
  "a" : "someOtherValue"
}

//Both of these objects can access property "a"

obj1.a //someValue
obj2.a //someOtherValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM