将标签附加到 JSON 响应
{title:'Alan', hasChild:true},
{title:'Alice', hasDetail:true},
{title:'Amos'},
{title:'Alonzo'},
{title:'Brad'},
{title:'Brent'},
{title:'Billy'},
{title:'Brenda'},
{title:'Callie'},
{title:'Cassie'},
{title:'Chris'},
这是我的 JSON 响应数据,现在我如何将 Header label
附加到每个数据的第一项。有些事情会让我区分这些是 A,这些是 B。
更改后的响应应该如下所示。
{title:'Alan', hasChild:true, header:'A'},
{title:'Alice', hasDetail:true},{title:'Alexander'},
{title:'Amos'},
{title:'Alonzo'},
{title:'Brad', header:'B'},
{title:'Brent'},
{title:'Billy'},
{title:'Brenda'},
{title:'Callie', header:'C'},
{title:'Cassie'},
{title:'Chris'},
{title:'Alan', hasChild:true},
{title:'Alice', hasDetail:true},
{title:'Amos'},
{title:'Alonzo'},
{title:'Brad'},
{title:'Brent'},
{title:'Billy'},
{title:'Brenda'},
{title:'Callie'},
{title:'Cassie'},
{title:'Chris'},
This is my JSON response data, now how would i append a Header label
to first items of each data. some thing likes which will make me differentiate that these are A, these are B.
The changed response should look like this.
{title:'Alan', hasChild:true, header:'A'},
{title:'Alice', hasDetail:true},{title:'Alexander'},
{title:'Amos'},
{title:'Alonzo'},
{title:'Brad', header:'B'},
{title:'Brent'},
{title:'Billy'},
{title:'Brenda'},
{title:'Callie', header:'C'},
{title:'Cassie'},
{title:'Chris'},
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您引用的代码/标记不是 JSON。 (JSON 要求属性名称使用双引号,字符串文字也使用双引号。)它看起来像是 JavaScript 数组文字中间的摘录,其中每个数组条目都由对象文字定义。例如:
我认为在你的问题中,你在第一个版本和第二个版本之间做了两件事:
如果您有对这些文字定义的数组的引用,则可以轻松添加属性(上面的#1):
这会在对象上的位置
0< 处创建一个新属性
header
/code> 在数组中。JavaScript 还有一个用于插入数组的函数,称为
splice< /code>
,您可以使用它插入到中间的数组中(上面的#2):
也就是说:从索引
2
(“Amos”条目)开始,删除 < code>0 元素,然后插入这个。改变已经到位。如果您拥有的确实是一个包含 JSON 的字符串,那么向其添加要么是字符串解析和拼接练习,要么是将 JSON 反序列化为对象,将属性添加到对象,然后将其重新序列化为 JSON再次。
下面是上面有效 JSON 形式的数组的示例:
这就像它出现在以 JSON 格式存储的文件中或在线路上一样。如果在程序代码中的字符串文字中,当然它需要采用该语言的适当字符串文字形式,并对双引号进行必要的转义(如果有)。例如,JSON 作为 JavaScript 字符串文字:
Your quoted code/markup is not JSON. (JSON requires that property names be in double quotes, and that string literals be in double quotes.) It looks like an excerpt from the middle of a JavaScript array literal, where each array entry is defined by object literals. E.g.:
I think in your question you've done two things between the first and second versions:
header
property to first entry ("Alan") (and to "Brad" and "Callie").If you have a reference to the array that those literals define, you can add properties (#1 above) easily enough:
That creates a new property,
header
, on the object at position0
in the array.JavaScript also has a function for inserting into an array, called
splice
, that you can use to insert into an array in the middle (#2 above):That says: Starting with index
2
(the "Amos" entry), remove0
elements, then insert this one. The change is made in place.If what you have is really a string, containing JSON, then adding to it is either a string parsing and splicing exercise, or a matter of deserializing the JSON into an object, adding the properties to the object, and re-serializing it to JSON again.
Here's an example of the array above in valid JSON form:
That's as it would appear in a file stored in JSON format, or on the wire. If within a string literal in program code, of course it would need to be in the appropriate string literal form for that language, with the necessary escaping — if any — for the double quotes. For instance, that JSON as a JavaScript string literal: