带有属性的JSON数组 [英] JSON array with properties

查看:196
本文介绍了带有属性的JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下js数组/对象

I have the following js array/object

var x = [1,2,3,4]; 
x.name = "myArray"; 

我正在使用json2.js而我正在尝试在字符串中序列化x。
我得到的就是数组:
[1,2,3,4]

I am using json2.js and I am trying to serialize x in a string. and all I get is the array: [1,2,3,4]

是正确的吗?
因为我可以将任何属性添加到数组中为什么json2不会处理它?
我缺少什么?

is that correct ? since I can add any property to an array why doesn't json2 handle that ? what am I missing ?

推荐答案


  1. 首先,json2.js会忽略属性数组。

  2. 如果它不能忽略它们那么就不可能有一个json格式的数组,这应该很容易评估。

让我们假设我们推出类似的东西:

Let's imagine we come out with something like:

[1,2,3,4]{name:'test',anotherProperty:'someValue'} 

如果以上是有效的javascript创建一个数组并坚持两个属性那么它会很棒我们可以json-it。
这相当于这样做:

if the above was valid javascript to create an array and stick two properties then it would be great and we could json-it. It would be equivalent to do this:

array = [1,2,3,4]
array.name = 'test';
array.anotherProperty = 'someValue'; 

但事实并非如此,这也是我们无法坚持json的原因。

But it is not and that's the reason why we can not persist into json.

这篇关于带有属性的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆