【发布时间】:2015-05-26 03:10:21
【问题描述】:
我很惊讶将 JSON 映射到数组中。
这里我提到了 JSON 数组。
[ { "_id":{ "$oid":"51d84a1e07121c7f442a6e76" }, "_type":[ "App_Model_Playlist_Content_Heading", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"Intro Activities" }, { "_id":{ "$oid":"51e073fd07121c9e4c15a843" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51e0732a07121cb24cbd36b4" } } }, { "_id":{ "$oid":"51d729ab07121c8b4014bd88" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51d7286a07121c443fc4f126" } } }, { "_id":{ "$oid":"51d84abd07121c4844cb9ab3" }, "_type":[ "App_Model_Playlist_Content_Heading", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"Book Discussion Groups" }, { "_id":{ "$oid":"51d84ade07121c0045ebe8c3" }, "_type":[ "App_Model_Playlist_Content_Narrative", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"Book discussion groups take a bit of front loading of information and definitely take practice. After each book group discussion it is necessary to come together as a class to discuss what worked well and what didn't work well. This allows students to reflect on what to do next time to make the group discussions more engaging and successful. There is also an assessment portion. If you would like students to reflect on each other's performance during group discussions. I like to use student recognitions for students to recognize other students for interesting comments or questions during discussion time." }, { "_id":{ "$oid":"51c36cbe07121c9b4f1af6d0" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51c36aa007121c524dc2a6d9" } } }, { "_id":{ "$oid":"51d84a3307121cee43dba549" }, "_type":[ "App_Model_Playlist_Content_Heading", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"Literary Analysis" }, { "_id":{ "$oid":"51d84a3f07121cee43ab551c" }, "_type":[ "App_Model_Playlist_Content_Narrative", "App_Model_Playlist_Content", "App_Model_Playlist" ], "text":"The literary analysis is used as a formative assessment. Students use the notes from the reading and their discussion groups to analyze an aspect from the story. Student choice is imperative in this part to inspire students to delve deeply into the story and critically think about the symbolism and themes explored throughout. " }, { "_id":{ "$oid":"51d724a907121c633c32d818" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51d7244507121cf33b5700e6" } } }, { "_id":{ "$oid":"51d723e007121cf33b304174" }, "_type":[ "App_Model_Playlist_Content_Resource", "App_Model_Playlist_Content", "App_Model_Playlist" ], "resource":{ "$ref":"resources", "$id":{ "$oid":"51d7238e07121cff3bf36405" } } } ]
Javascript:
尝试使用 javascript 获得预期结果。我在这里得到了一些输出,但是它与预期的结果不匹配。
var aa = JSON;
var array = [];
for(var i=0; i < aa.length; i++){
var t = {};
t.header = '';
t.narrative = '';
t.resource_id = '';
if(aa[i]._type[0]=='App_Model_Playlist_Content_Heading'){
t.header = aa[i].text;
}
if(aa[i]._type[0]=='App_Model_Playlist_Content_Narrative'){
t.narrative = aa[i].text;
}
if(aa[i]._type[0]=='App_Model_Playlist_Content_Resource'){
if(typeof aa[i].resource != 'undefined' && aa[i].resource != null && aa[i].resource !=''){
t.resource_id = aa[i].resource.$id.$oid;
}
}
array.push(t);
}
console.log(JSON.stringify(array));
下面我提到了数组中预期的 JSON。
预期结果:
[
{
"header": "Intro Activities",
"narrative": "",
"resource": "51e0732a07121cb24cbd36b4,51d7286a07121c443fc4f126"
},
{
"header": "Book Discussion Groups",
"narrative": "Book discussion groups take a bit of front loading of information and definitely take practice. After each book group discussion it is necessary to come together as a class to discuss what worked well and what didn't work well. This allows students to reflect on what to do next time to make the group discussions more engaging and successful. There is also an assessment portion. If you would like students to reflect on each other's performance during group discussions. I like to use student recognitions for students to recognize other students for interesting comments or questions during discussion time.",
"resource": "51c36aa007121c524dc2a6d9"
},
{
"header": "Literary Analysis",
"narrative": "The literary analysis is used as a formative assessment. Students use the notes from the reading and their discussion groups to analyze an aspect from the story. Student choice is imperative in this part to inspire students to delve deeply into the story and critically think about the symbolism and themes explored throughout.",
"resource": "51d7244507121cf33b5700e6,51d7238e07121cff3bf36405"
}
]
帮助我从上面的编码中得到预期的结果。 提前致谢。
【问题讨论】:
-
这段代码的结果是什么?我在小提琴上对其进行了测试,似乎有效。 jsfiddle.net/8vda06xw
-
@aSharma - 感谢您的快速回复,
-
@aSharma 感谢您的快速响应,我相信它可以正常工作,但在预期的结果中,第一个标题、第一个叙述以及直到下一个标题开始之前的资源,在第一个 JSON 数组中等等。
-
对不起,我无法理解这个问题。如果您可以粘贴实际结果并突出显示与预期的差异,那么我可能会提供帮助。
-
您的数据似乎是按顺序排列的,
Heading,然后是Narrative,然后是Resources 的列表,Narrative是可选的
标签: javascript json