我很震惊,在数组中映射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"
}
]
帮助我从上面的编码中获得预期的结果。 提前谢谢。
答案 0 :(得分:1)
你应该迭代JSON数组,并且如果你面对App_Model_Playlist_Content_Heading
或者currentObj为null,则创建一个新条目,因此它没有标题,就像这样
var array = [],
currentObj,
pushNew = function(header, narrative, resourceId) {
var newObj = {
header: header || '',
narrative: narrative || '',
resource_id: resourceId? [resourceId] : []
};
array.push(newObj);
return newObj;
}
;
for(var i=0; i < aa.length; i++){
// Starting of a new entry
if(aa[i]._type[0]=='App_Model_Playlist_Content_Heading'){
currentObj = pushNew(aa[i].text);
}
if(aa[i]._type[0]=='App_Model_Playlist_Content_Narrative'){
if(!currentObj) {
currentObj = pushNew(null, aa[i].text);
} else {
currentObj.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 !==''){
if(!currentObj) {
currentObj = pushNew(null, null, aa[i].resource.$id.$oid);
} else {
currentObj.resource_id.push(aa[i].resource.$id.$oid);
}
}
}
}
console.log(JSON.stringify(array));