我正在创建一个JSON对象,我在其中添加一个键和一个数组。 key和value的值都来自一个TreeSet,它具有排序形式的数据。然而,当我在我的json对象中插入数据时,它将被随机存储,没有任何顺序。 这是我的JSON对象目前:如何根据键对我的JSON对象进行排序?
{
"SPAIN":["SPAIN","this"],
"TAIWAN":["TAIWAN","this"],
"NORWAY":["NORWAY","this"],
"LATIN_AMERICA":["LATIN_AMERICA","this"]
}
,我的代码是:
Iterator<String> it= MyTreeSet.iterator();
while (it.hasNext()) {
String country = it.next();
System.out.println("----country"+country);
JSONArray jsonArray = new JSONArray();
jsonArray.put(country);
jsonArray.put("this);
jsonObj.put(country, jsonArray);
}
有没有什么办法可以将数据存储到while循环本身在我的JSON对象?
标准JSON对象是一组*无序*键/值对。它不能被“分类”。 –
顺便说一句,你的插图与你的代码不符。您的插图仅包含对象,不包含数组。 –
1.将它作为数组移动到Object 2.将您的数组排除 –