Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

javascript - 对象和数组的复杂JSON嵌套(Complex JSON nesting of objects and arrays)

I am having difficultly with syntax and structure of JSON objects/arrays.(我对JSON对象/数组的语法和结构很困难。)

{ "accounting" : [ { "firstName" : "John", "lastName" : "Doe", "age" : 23 }, { "firstName" : "Mary", "lastName" : "Smith", "age" : 32 } ], "sales" : [ { "firstName" : "Sally", "lastName" : "Green", "age" : 27 }, { "firstName" : "Jim", "lastName" : "Galley", "age" : 41 } ] } I want to make a nested structure of objects and arrays that would house the following info:(我想创建一个嵌套的对象和数组结构,它将包含以下信息:) { "problems": [{ "Diabetes":[{ "medications":[{ "medicationsClasses":[{ "className":[{ "associatedDrug":[{ "name":"asprin", "dose":"", "strength":"500 mg" }], "associatedDrug#2":[{ "name":"somethingElse", "dose":"", "strength":"500 mg" }] }], "className2":[{ "associatedDrug":[{ "name":"asprin", "dose":"", "strength":"500 mg" }], "associatedDrug#2":[{ "name":"somethingElse", "dose":"", "strength":"500 mg" }] }] }] }], "labs":[{ "missing_field": "missing_value" }] }], "Asthma":[{}] }]} But I have no idea what the right way to do this should be.(但我不知道这样做的正确方法是什么。) Should I just be making JavaScript objects?(我应该只是制作JavaScript对象吗?) Does JSON make sense for this project?(JSON对这个项目有意义吗?) What is the correct syntax to set something like this up?(设置这样的东西的正确语法是什么?) Here is my code so far:(到目前为止,这是我的代码:) $(document).ready(function() { $.getJSON('js/orders.json', function(json) { $.each(json.problems, function(index, order) { $('.loadMeds').append('<p>' + order.name + '</p>') }); }); });   ask by Alex translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The first code is an example of Javascript code, which is similar, however not JSON.(第一个代码是Javascript代码的示例,它类似,但不是JSON。)

JSON would not have 1) comments and 2) the var keyword(JSON不会有1)注释和2) var关键字) You don't have any comments in your JSON, but you should remove the var and start like this:(你的JSON中没有任何注释,但你应该删除var并像这样开始:) orders: { The [{}] notation means "object in an array" and is not what you need everywhere.([{}]符号表示“数组中的对象”,并不是您需要的所有内容。) It is not an error, but it's too complicated for some purposes.(这不是错误,但对于某些目的而言太复杂了。) AssociatedDrug should work well as an object:(AssociatedDrug应该作为一个对象很好地工作:) "associatedDrug": { "name":"asprin", "dose":"", "strength":"500 mg" } Also, the empty object labs should be filled with something.(此外,空对象实验室应该填充一些东西。) Other than that, your code is okay.(除此之外,你的代码还可以。) You can either paste it into javascript, or use the JSON.parse() method, or any other parsing method (please don't use eval )(您可以将其粘贴到javascript中,也可以使用JSON.parse()方法或任何其他解析方法(请不要使用eval )) Update 2 answered:(更新2回答:) obj.problems[0].Diabetes[0].medications[0].medicationsClasses[0].className[0].associatedDrug[0].name returns 'aspirin'.(返回'阿司匹林'。) It is however better suited for foreaches everywhere(然而,它更适合各地的森林)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...