简体   繁体   English

jQuery的多级JSON

[英]Multi Level JSON with jQuery

I am trying to create some json that is a few levels deep, and I am having trouble in 2 places. 我正在尝试创建一些深度较深的json,并且在2个地方遇到了麻烦。 The first, is getting the format correct. 首先,要使格式正确。 I also want to add a "level_two" to the data set. 我还想向数据集添加一个“ level_two”。

The second is parsing the data. 第二个是解析数据。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks! 谢谢!

JSON:
{
    "math_problems": [
        {
            "level_one": {
                "addition": {
                   "problem": {
                        "data_set": [
                            3,
                            2
                        ]
                    },
                    "problem": {
                        "data_set": [
                            5,
                            1
                        ]
                    }
                },
                "subtraction": {
                    "problem": {
                        "data_set": [
                            4,
                            2
                        ]
                    }
                },
                "division": {
                    "problem": {
                        "data_set": [
                            3,
                            2
                        ]
                    }
                },
                "multiplication": {
                    "problem": {
                        "data_set": [
                            3,
                            2
                        ]
                    }
                }
            }
        }
    ]
}


jQuery:

$.getJSON('/data/math.json', function(data) {

        var the_data = [];

        $.each(data,function(index,item) {
            i = index;
            number_one = item[0]['level_one']['addition']['problem']['data_set'][0];
            number_two = item[0]['level_one']['addition']['problem']['data_set'][1];

            the_data += number_one + " + " + number_two;
        });

        $('<ul/>', {
            'class': 'my-new-list',
            html: the_data + " = " + (number_one + number_two)
        }).appendTo('body');
    });

I want to be able to parse the data to pull out say: 我想能够解析出数据说:

level_one > addition > problem 1 > 3, 2 level_one>加法>问题1> 3,2

level_two > subtraction > problem 1 > 4, 3 level_two>减法>问题1> 4,3

Any ideas? 有任何想法吗?

The problem is, you have the same key defined in a hash at the same level: 问题是,您在同一级别的哈希中定义了相同的键:

"problem": {
    "data_set": [
        3,
        2
    ]
},
"problem": {
    "data_set": [
        5,
        1
    ]
}

These will collide and overwrite each other. 这些将相互碰撞并相互覆盖。 Restructure it to be an array, and you should be good. 将其重组为一个数组,您应该会很好。

So the correct JSON is: 因此正确的JSON是:

var object = {
    levelOne: {
        // etc.
    },
    levelTwo: {
        // etc.
    }
}

Parsing it is done using JSON.parse() and then traversing it is like: 使用JSON.parse()解析它,然后遍历它就像:

object.level_one.prop.prop1;
object.level_two.otherProp.otherProp2;

JSON gets converted into a javascript object, so the fact that you repeat the key "problem" on the "addition" makes the second "problem" overwrite the first. JSON被转换为javascript对象,因此您在“ addition”上重复键“ problem”的事实使第二个“问题”覆盖了第一个问题。

You should rethink your json structure, maybe something like: 您应该重新考虑json结构,也许是这样的:

"math_problems" : {
  "level_one": {
    "addition": {
      "problems": [
        {
          "data_set": [3,2]
        },
        {
          "data_set": [5,1]
        }
      ]
    },
    "subtraction": ...     
  }  
}

or, in case the problems, problem types and levels are highly dynamic: 或者,如果问题,问题类型和级别是高度动态的:

"math_problems" : {    
  "levels": [
    {
      "name" : "Level one",
      "problems": [
        {
          "type" : "addition",
          "data_set": [3,2]
        },
        {
          "type" : "addition",
          "data_set": [5,1]
        },
        {
          "type" : "subtraction",
          "data_set": [2,1]
        }
      ]
    },
    {
      "name" : "Level two",
      "problems": [
        ...
      ]
    }
  ]
}

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

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