如何在android中排序/排列JSON对象键? [重复]

时间:2022-11-23 08:54:30

Possible Duplicate:
Sort JavaScript object by key

可能重复:按键排序JavaScript对象

I am creating JSONArray from JSONObject in android. The JSONArray that i wanted to be is: [{"last_name":"cruz", "first_name":"juan", "middle_name":"sam"}]

我在android中从JSONObject创建JSONArray。我想成为的JSONArray是:[{“last_name”:“cruz”,“first_name”:“juan”,“middle_name”:“sam”}]

but it appears [{"first_name":"cruz", "last_name":"juan", "middle_name":"sam"}]

但它出现[{“first_name”:“cruz”,“last_name”:“juan”,“middle_name”:“sam”}]

how can I arrange the array in order that I wanted?

我怎样才能按照我想要的顺序排列阵列?

Thanks...

谢谢...

2 个解决方案

#1


5  

1. prepare a LinkedHashMap object with elements

2. convert it to JSONObject

Example:

例:

Map obj = new LinkedHashMap();
obj.put("a", "String1");
obj.put("b", new Integer(1));
obj.put("c", new Boolean(true));
obj.put("d", "String2");
JSONObject json = new JSONObject(obj);

download this library:

下载此库:

https://github.com/douglascrockford/JSON-java

https://github.com/douglascrockford/JSON-java

save all the files in a new package in your project

将所有文件保存在项目的新包中

instead of using org.json.JSONObject use your.package.JSONObject which you added from the downloaded library.

而不是使用org.json.JSONObject使用从下载的库中添加的your.package.JSONObject。

now open JSONObject.java file and change HashMap() to LinkedHashMap() in the constructor

现在打开JSONObject.java文件并在构造函数中将HashMap()更改为LinkedHashMap()

public JSONObject(Map map)

This will make the JSONObject store data in the order you entered the values using put.

这将使JSONObject按照您使用put输入值的顺序存储数据。

#2


0  

You can't maintain the order of JSONObject response, because it is itself mentioned in the documents.

您无法维护JSONObject响应的顺序,因为它本身在文档中提到。

An object is an unordered set of name/value pairs.

对象是一组无序的名称/值对。

I had faced the same problem and then i need to use either the gson library or to make json by ur logic..

我遇到了同样的问题,然后我需要使用gson库或通过你的逻辑制作json ..

An object is an unordered set of name/value pairs.

Best of luck.!

祝你好运。!

#1


5  

1. prepare a LinkedHashMap object with elements

2. convert it to JSONObject

Example:

例:

Map obj = new LinkedHashMap();
obj.put("a", "String1");
obj.put("b", new Integer(1));
obj.put("c", new Boolean(true));
obj.put("d", "String2");
JSONObject json = new JSONObject(obj);

download this library:

下载此库:

https://github.com/douglascrockford/JSON-java

https://github.com/douglascrockford/JSON-java

save all the files in a new package in your project

将所有文件保存在项目的新包中

instead of using org.json.JSONObject use your.package.JSONObject which you added from the downloaded library.

而不是使用org.json.JSONObject使用从下载的库中添加的your.package.JSONObject。

now open JSONObject.java file and change HashMap() to LinkedHashMap() in the constructor

现在打开JSONObject.java文件并在构造函数中将HashMap()更改为LinkedHashMap()

public JSONObject(Map map)

This will make the JSONObject store data in the order you entered the values using put.

这将使JSONObject按照您使用put输入值的顺序存储数据。

#2


0  

You can't maintain the order of JSONObject response, because it is itself mentioned in the documents.

您无法维护JSONObject响应的顺序,因为它本身在文档中提到。

An object is an unordered set of name/value pairs.

对象是一组无序的名称/值对。

I had faced the same problem and then i need to use either the gson library or to make json by ur logic..

我遇到了同样的问题,然后我需要使用gson库或通过你的逻辑制作json ..

An object is an unordered set of name/value pairs.

Best of luck.!

祝你好运。!