转换JSON以获得新的顶级包装器

时间:2013-09-04 18:34:27

标签: javascript json mustache

我怀疑我使用了错误的术语,所以请耐心等待。

我一直在使用mustache.js非常有效地处理API的JSON输出。我正在使用一个兄弟API,其外观看起来大致相似,除了没有顶级名称。我试图弄清楚如何将新的JSON输出调整为其他格式,这样我可以继续使用我的mustache.js方法(这似乎更容易选择,另一种是使用mustache.js以外的东西,这是我想避免的。)

我可以使用mustache.js使用顶级名称'records'在以下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}
}

{"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} }

我无法迭代的格式,因为它缺少顶级名称,是:

[{"id":"x","user": {"id":"x","first_name":"x","last_name":"x"},"title":"Test Post 3","text":"This is an equally cool and enthralling post.","created_at":"2013-08-29T17:46:04.801Z","updated_at":"2013-08-29T17:46:04.804Z","num_comments":0,"num_likes":0},
{"id":"y","user":   {"id":"212342277","first_name":"x","last_name":"x"},"title":"Test Post 1","text":"Super cool content you want to read!","created_at":"2013-08- 29T17:44:58.188Z","updated_at":"2013-08-29T17:44:58.190Z","num_comments":0,"num_likes":0}]

我尝试了以下方法将第二个JSON示例按照第一个格式进行按摩:

[{"id":"x","user": {"id":"x","first_name":"x","last_name":"x"},"title":"Test Post 3","text":"This is an equally cool and enthralling post.","created_at":"2013-08-29T17:46:04.801Z","updated_at":"2013-08-29T17:46:04.804Z","num_comments":0,"num_likes":0}, {"id":"y","user": {"id":"212342277","first_name":"x","last_name":"x"},"title":"Test Post 1","text":"Super cool content you want to read!","created_at":"2013-08- 29T17:44:58.188Z","updated_at":"2013-08-29T17:44:58.190Z","num_comments":0,"num_likes":0}]

我有一个警告,弹出var'stuff'中的内容,它似乎被格式化为第一个JSON示例,但是mustache.js不解析它(我已经验证了mustache.js执行)。在尝试解决任何其他问题之前,我想了解如何在上面的AJAX调用成功属性中向JSON数组添加顶级名称是否正确,或者我是否必须采用不同的方式。

如果我遗漏了一些有助于解释此事的内容,请告诉我,我可以添加它。

0 个答案:

没有答案