Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 恢复将数据放入JSONObject的顺序_Android_Json - Fatal编程技术网

Android 恢复将数据放入JSONObject的顺序

Android 恢复将数据放入JSONObject的顺序,android,json,Android,Json,我希望在代码中插入json,但它的工作原理有所不同!我首先想要CustomerID,然后是Name,但是这个json首先给出Name,然后是CustomerID。我已经插入了json作为我想要的,但为什么它会给出不同的结果,请帮助我 JSONObject json = new JSONObject(); try { json.put("CustomerID", "069C21F1-EE87-4FB4-A129-478AEAA454FF"); js

我希望在代码中插入json,但它的工作原理有所不同!我首先想要
CustomerID
,然后是
Name
,但是这个json首先给出
Name
,然后是
CustomerID
。我已经插入了json作为我想要的,但为什么它会给出不同的结果,请帮助我

    JSONObject json = new JSONObject();

    try {
        json.put("CustomerID", "069C21F1-EE87-4FB4-A129-478AEAA454FF");
        json.put("Name", "Name_" + (int) Math.random() * 1000);
} catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

您可以将该名称放在CustomerID行之前,以满足自己的需求,但这根本不会有任何区别。因为json中的数据是以键值对的形式提取的。它独立于它所处的顺序。

这是JSONObject的预期行为。根据定义,它说:

作为无序名称/值对集的对象

但是,如果要订购,请执行以下操作:

1. prepare a LinkedHashMap object with elements

2. convert it to JSONObject
示例:

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);
编辑:

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);
下载此库:

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

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

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

public JSONObject(Map map)
您可以使用

请参见

如果你是你的朋友。这将把有序映射打印成有序JSON字符串

我使用了最新版本的Gson(2.3.1),您可以通过本文底部的以下选项下载它

import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;

public class OrderedJson {
    public static void main(String[] args) {
        // Create a new ordered map.
        Map<String,String> myLinkedHashMap = new LinkedHashMap<String, String>();

        // Add items, in-order, to the map.
        myLinkedHashMap.put("1", "first");
        myLinkedHashMap.put("2", "second");
        myLinkedHashMap.put("3", "third");

        // Instantiate a new Gson instance.
        Gson gson = new Gson();

        // Convert the ordered map into an ordered string.
        String json = gson.toJson(myLinkedHashMap, LinkedHashMap.class);

        // Print ordered string.
        System.out.println(json); // {"1":"first","2":"second","3":"third"}
    }
}

或者您可以访问以获得更多下载选项。

ya我已经尝试过了,但我想在插入json时生成它。。你知道怎么做吗?我现在不使用,但在调试过程中,我发现顺序没有得到维护,根据要求,我需要它们按顺序排列。为什么你应该关心顺序,JsonObject使用HashMap来存储数据,所以,如果你想要一个值,你应该为它提供一个键,实际上我会把这个json发布到url,并且在服务器上,它会被按顺序解析,所以…我知道,谢谢。但是我如何通过这个LinkedHashMap制作JsonArray呢?请帮帮我。。
compile 'com.google.code.gson:gson:2.3.1'