将标签附加到 JSON 响应

发布于 2024-11-05 11:43:10 字数 745 浏览 0 评论 0原文

{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2024-11-12 11:43:10

您引用的代码/标记不是 JSON。 (JSON 要求属性名称使用双引号,字符串文字也使用双引号。)它看起来像是 JavaScript 数组文字中间的摘录,其中每个数组条目都由对象文字定义。例如:

var people = [
    {title:'Alan', hasChild:true},
    {title:'Alice', hasDetail:true},
    {title:'Amos'},
    {title:'Alonzo'}
];

我认为在你的问题中,你在第一个版本和第二个版本之间做了两件事:

  1. 你在第一个条目(“Alan”)(以及“Brad”和“凯丽”)。
  2. 您在“Alice”和“Amos”之间添加了新的“Alexander”条目。

如果您有对这些文字定义的数组的引用,则可以轻松添加属性(上面的#1):

people[0].header = 'A';

这会在对象上的位置 0< 处创建一个新属性 header /code> 在数组中。

JavaScript 还有一个用于插入数组的函数,称为 splice< /code>,您可以使用它插入到中间的数组中(上面的#2):

people.splice(2, 0, {title: 'Alexander'});

也就是说:从索引 2 (“Amos”条目)开始,删除 < code>0 元素,然后插入这个。改变已经到位。

如果您拥有的确实是一个包含 JSON 的字符串,那么向其添加要么是字符串解析和拼接练习,要么是将 JSON 反序列化为对象,将属性添加到对象,然后将其重新序列化为 JSON再次。


下面是上面有效 JSON 形式的数组的示例:

[
    {"title":"Alan", "hasChild":true},
    {"title":"Alice", "hasDetail":true},
    {"title":"Amos"},
    {"title":"Alonzo"}
]

这就像它出现在以 JSON 格式存储的文件中或在线路上一样。如果在程序代码中的字符串文字中,当然它需要采用该语言的适当字符串文字形式,并对双引号进行必要的转义(如果有)。例如,JSON 作为 JavaScript 字符串文字:

var jsonString = '[' +
    '{"title":"Alan", "hasChild":true},' +
    '{"title":"Alice", "hasDetail":true},' +
    '{"title":"Amos"},' +
    '{"title":"Alonzo"}' +
']';

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.:

var people = [
    {title:'Alan', hasChild:true},
    {title:'Alice', hasDetail:true},
    {title:'Amos'},
    {title:'Alonzo'}
];

I think in your question you've done two things between the first and second versions:

  1. You've added a header property to first entry ("Alan") (and to "Brad" and "Callie").
  2. You've added a new "Alexander" entry between "Alice" and "Amos".

If you have a reference to the array that those literals define, you can add properties (#1 above) easily enough:

people[0].header = 'A';

That creates a new property, header, on the object at position 0 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):

people.splice(2, 0, {title: 'Alexander'});

That says: Starting with index 2 (the "Amos" entry), remove 0 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:

[
    {"title":"Alan", "hasChild":true},
    {"title":"Alice", "hasDetail":true},
    {"title":"Amos"},
    {"title":"Alonzo"}
]

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:

var jsonString = '[' +
    '{"title":"Alan", "hasChild":true},' +
    '{"title":"Alice", "hasDetail":true},' +
    '{"title":"Amos"},' +
    '{"title":"Alonzo"}' +
']';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文