Javascript 将JSON转换为具有新的顶级包装器
我怀疑我使用了错误的术语,所以请容忍我 我一直在非常有效地使用mustache.js处理API的JSON输出。我正在使用一个兄弟API,它的outlook看起来非常相似,只是没有顶级名称。我试图找出如何将新的JSON输出调整为其他格式,这样我就可以继续使用我的mustache.js方法(这似乎是比较容易的选项,另一个是使用mustache.js以外的其他格式,我正在努力避免) 我可以使用mustache.js使用顶级名称“records”非常轻松地迭代以下JSON正文:Javascript 将JSON转换为具有新的顶级包装器,javascript,json,mustache,Javascript,Json,Mustache,我怀疑我使用了错误的术语,所以请容忍我 我一直在非常有效地使用mustache.js处理API的JSON输出。我正在使用一个兄弟API,它的outlook看起来非常相似,只是没有顶级名称。我试图找出如何将新的JSON输出调整为其他格式,这样我就可以继续使用我的mustache.js方法(这似乎是比较容易的选项,另一个是使用mustache.js以外的其他格式,我正在努力避免) 我可以使用mustache.js使用顶级名称“records”非常轻松地迭代以下JSON正文: {"records":
{"records":
[{"dt_created":"08/23/2013 04:49:13 PM","created_by":"x, x","dt_updated":"08/23/2013 04:49:13 PM","updated_by":"x, x","field_60374": ["Project 67"],"field_60331":["Ability to adjust the number of lines dynamically based on the mood of the person 3 rows down, 4th cube, 11th building. A breakthrough in mind-control data processing."],"field_60333": [{"id":"x","first":"x","last":"x"}],"field_60394": [{"id":"x","first":"x","last":"x"}],"field_60414":["11"],"field_60375": ["22"],"field_60395":["A"],"record_id":"1920704","form_id":"10898"},
{"dt_created":"08/23/2013 04:47:45 PM","created_by":"x, x","dt_updated":"08/23/2013 04:47:45 PM","updated_by":"x, x","field_60374":["Project 2"],"field_60331":["Very cool project to allow more than a single invoice to be in an ERP at any one time. Quite a big leap forward."],"field_60333": [{"id":"x","first":"x","last":"x"}],"field_60394": [{"id":"x","first":"x","last":"x"}],"field_60414":["x, x"],"field_60375":["60"],"field_60395": ["A"],"record_id":"1920703","form_id":"10898"}],
"meta":{"total":2,"count":2}
}
$.ajax({
[…….],
success: function(json_data){
alert("success");
var template=$('#listPosts').html();
var stuff = {"records":json_data}; //here
var stuff = JSON.stringify(stuff); //here
alert(stuff);
//var html = Mustache.to_html(template, json_data);
var html = Mustache.to_html(template, stuff);
//$('.content-pane').empty();
$('#listOfPostsContainer').html(html);
}
}
由于缺少顶级名称,我无法迭代的格式是:
我尝试了以下方法将第二个JSON示例转换为第一个JSON示例的格式:
{"records":
[{"dt_created":"08/23/2013 04:49:13 PM","created_by":"x, x","dt_updated":"08/23/2013 04:49:13 PM","updated_by":"x, x","field_60374": ["Project 67"],"field_60331":["Ability to adjust the number of lines dynamically based on the mood of the person 3 rows down, 4th cube, 11th building. A breakthrough in mind-control data processing."],"field_60333": [{"id":"x","first":"x","last":"x"}],"field_60394": [{"id":"x","first":"x","last":"x"}],"field_60414":["11"],"field_60375": ["22"],"field_60395":["A"],"record_id":"1920704","form_id":"10898"},
{"dt_created":"08/23/2013 04:47:45 PM","created_by":"x, x","dt_updated":"08/23/2013 04:47:45 PM","updated_by":"x, x","field_60374":["Project 2"],"field_60331":["Very cool project to allow more than a single invoice to be in an ERP at any one time. Quite a big leap forward."],"field_60333": [{"id":"x","first":"x","last":"x"}],"field_60394": [{"id":"x","first":"x","last":"x"}],"field_60414":["x, x"],"field_60375":["60"],"field_60395": ["A"],"record_id":"1920703","form_id":"10898"}],
"meta":{"total":2,"count":2}
}
$.ajax({
[…….],
success: function(json_data){
alert("success");
var template=$('#listPosts').html();
var stuff = {"records":json_data}; //here
var stuff = JSON.stringify(stuff); //here
alert(stuff);
//var html = Mustache.to_html(template, json_data);
var html = Mustache.to_html(template, stuff);
//$('.content-pane').empty();
$('#listOfPostsContainer').html(html);
}
}
我有一个警报,弹出var'stuff'中的内容,它的格式似乎与第一个JSON示例类似,但mustache.js没有解析它(我已经验证了mustache.js是否执行)。在尝试解决任何其他问题之前,我想了解我在上述AJAX call success属性中向JSON数组添加顶级名称的方式是否正确,或者我是否必须以不同的方式进行
如果我缺少一些东西来帮助解释这一点,请告诉我,这样我就可以添加它。什么是
JSON.stringify
在那里做什么?Mustach想要一个JS对象,而JSON.stringify
正在将其转换为字符串。抛开那条线,它可能会起作用。我想是出了什么问题。事实上,在谷歌搜索了很久之后,我终于找到了正确的帖子。我可以绕开我的问题——在这种情况下,通过在mustach中使用(.),这样我就可以迭代第二个JSON响应:{{{{{}}{{}}{{/}}我感谢您抽出时间阅读本文。数字一旦我放弃并发布一个问题,我就会把它弄清楚