JSONObject:为什么JSONObject改变属性的顺序 [英] JSONObject : Why JSONObject changing the order of attributes
本文介绍了JSONObject:为什么JSONObject改变属性的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用JSON对象构造一个JSON字符串
I was trying to construct an JSON String using JSON Object
我希望以这种方式构造JSON字符串
I want the JSON String to be constructed this way
{
"Level": "3",
"Name": "testLogger",
"IPADDRESS": "testMachiene",
"Message": "hiiiiiiiiii",
"TimeStamp": "test12345678"
}
这是我的简单程序
package com;
import org.json.JSONObject;
public class Teste {
public static void main(String args[]) throws Exception {
int loglevel = 3;
String loggerName = "testLogger";
String machieneName = "testMachiene";
String timeStamp = "test12345678";
String message = "hiiiiiiiiii";
JSONObject obj = new JSONObject();
obj.put("TimeStamp", message);
obj.put("Message", timeStamp);
obj.put("IPADDRESS", machieneName);
obj.put("Name", loggerName);
obj.put("Level", loglevel);
System.out.println(obj.toString());
}
}
它是以这种方式构建
{
"Name": "testLogger",
"TimeStamp": "hiiiiiiiiii",
"Message": "test12345678",
"Level": 3,
"IPADDRESS": "testMachiene"
}
我的问题是为什么它改变了属性的顺序
My question is that why its changing the order of attributes
我可以订购吗?我希望在哪里?
Can i have the order in which i wish ??
推荐答案
请在此处查看答案: JSON订单混乱
你不能也不应该依赖于
JSON对象中元素的排序。
You cannot and should not rely on the ordering of elements within a JSON object.
来自 http://www.json.org/ :
对象是一组无序的名称/值对
"An object is an unordered set of name/value pairs"
因此,JSON图书馆员es可以自由地重新排列元素的顺序。这不是错误。
As a consequence, JSON libraries are free to rearrange the order of the elements as they see fit. This is not a bug.
这篇关于JSONObject:为什么JSONObject改变属性的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文