【问题标题】:Restore the order in which data is put into a JSONObject恢复将数据放入 JSONObject 的顺序
【发布时间】:2012-09-22 05:42:06
【问题描述】:

我想在这段代码中插入 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();
    }

【问题讨论】:

  • 我现在没有使用,但在调试中我看到订单没有得到维护,根据要求我需要他们按顺序排列
  • 为什么要关心订单,JsonObject 使用 HashMap 来存储数据,所以如果你想要一个值,你应该为它提供一个键
  • 实际上我将此 json 发布到 url 并在服务器上按顺序对其进行解析...

标签: android json


【解决方案1】:

这是 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);

编辑:

下载这个库:

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

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

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

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

public JSONObject(Map map)

【讨论】:

  • 我知道,谢谢。但是我怎样才能通过这个 LinkedHashMap 制作 JsonArray 呢?请帮帮我..
【解决方案2】:

您可以将该名称放在 CustomerID 行之前以满足您自己的需要,但这根本不会产生影响。因为json中的数据是以键值对的形式提取出来的。它独立于其放置的顺序。

【讨论】:

  • 是的,我试过了,但我想在插入时制作 json.. 你知道怎么做吗?
【解决方案3】:

你可以使用 accumulative()

android developpers refference

【讨论】:

    【解决方案4】:

    A useful answer here

    Gson 如果你的朋友。这会将有序的地图打印成有序的 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"}
        }
    }
    

    依赖选项 马文

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
    </dependency>
    

    Gradle

    compile 'com.google.code.gson:gson:2.3.1'
    

    或者您可以访问Maven Central 了解更多下载选项。

    【讨论】:

      猜你喜欢
      • 2014-05-28
      • 1970-01-01
      • 2015-06-29
      • 2020-01-23
      • 1970-01-01
      • 2015-09-12
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多