如何生成JSON字符串形式LinkedHashMap ?

时间:2022-11-24 22:09:37

How can i generate JSON String Form linkedHashMap.

如何从linkedHashMap中生成JSON字符串。

map contains (1,aaa) (2,bbb), (3,ccc)... from this i need to generate Json string

地图包含(1,aaa) (2,bbb), (3,ccc)…我需要从中生成Json字符串

{ {id:1,text:"aaa"},{id:2,text:"bbb"},{id:3,text:"ccc"},...}

{ { id:1、文本:“aaa”},{ id:2、文本:" bbb " },{ id:3、文本:“ccc”},…}

Please any one can help me to fix this.

请谁能帮我把这个修好。

1 个解决方案

#1


0  

In javascript a map is acutally pretty much the same as a classical object:

在javascript中,map与经典的对象非常相似:

{ "1": "aaa", "2":"bbb","3":"ccc",... }

I am not 100% sure you can use numbers as keys...

我不能百分之百地肯定你能把数字当作钥匙……

an alternative would be to use a structure like this and have an array of objects:

另一种选择是使用这样的结构并拥有一系列对象:

[ {id:1,text:"aaa"},{id:2,text:"bbb"},{id:3,text:"ccc"},... ]

or to be more in the spirit of a map, you should name the fields "map-like":

或者更符合地图的精神,你应该把这些字段命名为“类似地图”:

[ {key:1,value:"aaa"},{key:2,value:"bbb"},{key:3,value:"ccc"},... ]

All 3 are valid json/javascript forms, which one suits you best depends on what you actually want to use it for.

这3种都是有效的json/javascript表单,哪个最适合您,取决于您实际想要使用它做什么。

#1


0  

In javascript a map is acutally pretty much the same as a classical object:

在javascript中,map与经典的对象非常相似:

{ "1": "aaa", "2":"bbb","3":"ccc",... }

I am not 100% sure you can use numbers as keys...

我不能百分之百地肯定你能把数字当作钥匙……

an alternative would be to use a structure like this and have an array of objects:

另一种选择是使用这样的结构并拥有一系列对象:

[ {id:1,text:"aaa"},{id:2,text:"bbb"},{id:3,text:"ccc"},... ]

or to be more in the spirit of a map, you should name the fields "map-like":

或者更符合地图的精神,你应该把这些字段命名为“类似地图”:

[ {key:1,value:"aaa"},{key:2,value:"bbb"},{key:3,value:"ccc"},... ]

All 3 are valid json/javascript forms, which one suits you best depends on what you actually want to use it for.

这3种都是有效的json/javascript表单,哪个最适合您,取决于您实际想要使用它做什么。