如何组合两个JSONArrays JAVA(以下是特殊要求)
我有一个将两个JSONArray组合成一个的代码片段。在这里如何组合两个JSONArrays JAVA(以下是特殊要求),java,arrays,json,Java,Arrays,Json,我有一个将两个JSONArray组合成一个的代码片段。在这里 for (i = 0; i < cityJArray.length(); i++) { resultsJArray.put(cityJArray.getJSONObject(i)); } 我已经将其转换为JSONArray,因为我必须将字段“city”分开,并将其属性名称更改为“city\u id”、“city\u name”、“city\u slug”。因为它们将创建一个副本,因为它已经是“results”字段中的
for (i = 0; i < cityJArray.length(); i++)
{
resultsJArray.put(cityJArray.getJSONObject(i));
}
我已经将其转换为JSONArray,因为我必须将字段“city”分开,并将其属性名称更改为“city\u id”、“city\u name”、“city\u slug”。因为它们将创建一个副本,因为它已经是“results”字段中的一个属性(请参阅给定的JSONObject)
(用途:从JSONObject创建.CSV文件)
(问题开始了:当我试图将一个字段从JSONObject中分离出来,使其成为不同的列时)
JSONArrays详细信息:
结果Jarray:
[{"id": 248,
"name": "Alternatif Cibubur",
"slug": "alternatif-cibubur",
"status": "active",
"longitude": 106.900786}]
城市雅雷:
[{"id": 11,
"name": "Depok",
"slug": "depok"
}]
我想将这两个独立的JSONArray添加到一个类似这样的JSONArray中(下面是预期的输出)
这应该可以按要求工作,我注意到两个数组最初都是字符串,然后都转换为JSONArray
String resultsString = "[{ 'id': 248, 'name': 'Alternatif Cibubur', 'slug': 'alternatif-cibubur', 'status': 'active', 'longitude': 106.900786 }]";
String cityString = "[{ 'id': 11, 'name': 'Depok', 'slug': 'depok' }]";
try
{
//Convert String into JSONArray for both results and city.
JSONArray resultsJArray = new JSONArray(resultsString);
JSONArray cityJArray = new JSONArray(cityString);
//Get the first and only JSONObject within each of the arrays.
JSONObject resultsJObject = resultsJArray.getJSONObject(0);
JSONObject cityJObject = cityJArray.getJSONObject(0);
//Get the required values from the JSONObject within city.
int city_id = cityJObject.getInt("id");
String city_name = cityJObject.getString("name");
String city_slug = cityJObject.getString("slug");
//Put the values into the results JSONObject.
resultsJObject.put("city_id", city_id);
resultsJObject.put("city_name", city_name);
resultsJObject.put("city_slug", city_slug);
//Print to verify.
System.out.println("Updated resultsJArray: " + resultsJArray);
}
catch (JSONException e)
{
e.printStackTrace();
}
我使用包“com.alibaba.fastjson”,代码如下:
public static void CombinTest(){
String a = "[{\"id\": 248, \n" +
"\"name\": \"Alternatif Cibubur\", \n" +
"\"slug\": \"alternatif-cibubur\", \n" +
"\"status\": \"active\", \n" +
"\"longitude\": 106.900786}]";
String b = "[{\"id\": 11, \n" +
"\"name\": \"Depok\", \n" +
"\"slug\": \"depok\"\n" +
"}]";
JSONArray resultObjArr = JSON.parseArray(a);
JSONArray cityObjArr = JSON.parseArray(b);
for (int i = 0; i < resultObjArr.size(); i++)
{
JSONObject city = cityObjArr.getJSONObject(i);
Set<String> keys = city.keySet();
if(keys.size()>0){
for(String key:keys){
resultObjArr.getJSONObject(i).put("city_"+key,city.get(key));
}
}
}
System.out.println(JSON.toJSONString(resultObjArr));
}
公共静态测试(){
字符串a=“[{\“id\”:248\n”+
“\”名称\“:\”替代Cibubur \“,\n”+
“\”slug\“:\”alternatif cibubur\“,\n”+
“\”状态\“:\”活动\“,\n”+
“\”经度\“:106.900786}]”;
字符串b=“[{\“id\”:11\n”+
“\'name\:\'Depok\”,\n”+
“\“slug\”:\“depok\”\n”+
"}]";
JSONArray resultObjArr=JSON.parseArray(a);
JSONArray cityobjar=JSON.parseArray(b);
对于(int i=0;i0){
用于(字符串键:键){
结果jarr.getJSONObject(i.put)(“city_”+key,city.get(key));
}
}
}
System.out.println(JSON.toJSONString(resultObjArr));
}
如上所示,resultsJArray
实际上是一个JSONObject(因为它被封装在{…}
中,不清楚cityJArray
是什么(在的“slug”后面还有一个逗号):“depok”,}
这似乎不正确。对不起,slug后面没有逗号。我现在已经编辑了它。实际上,JSONArray是用[{…}]括起来的
。对于这些错误,我深表歉意。请更新您上面问题中的resultsJArray
和cityJArray
,以便清楚它们是什么类型的。好的,当然,先生,我已经更新了两个JSONArray。谢谢您的回答。我会尝试并尽快更新您。我已经尝试了答案,经过一些编辑后效果良好。我有一个m还有一个问题是如何将JSONObjectresultsJObject
转换为新的JSONArray(JSONArray resultsJArrayFinal;
)?JSONArray resultsJArrayFinal=new JSONArray();
后跟resultsJArrayFinal.put(resultsJObject)
谢谢你的回答,先生。我对上面的代码有一个疑问,那就是我如何才能将JSONObject aoobject
解析到JSONArray resultObjArr
而不获得字符串“results”?因为我有2个JSONArray,并且我已经将其转换为字符串。因此“results”字段名已经消失,我无法解析JSONO通过get()
string方法获取对象。您能告诉我是否有任何方法可以纠正它吗?我更改了代码,如上所述,我使用了您的2个JSONArray作为初始数据
String resultsString = "[{ 'id': 248, 'name': 'Alternatif Cibubur', 'slug': 'alternatif-cibubur', 'status': 'active', 'longitude': 106.900786 }]";
String cityString = "[{ 'id': 11, 'name': 'Depok', 'slug': 'depok' }]";
try
{
//Convert String into JSONArray for both results and city.
JSONArray resultsJArray = new JSONArray(resultsString);
JSONArray cityJArray = new JSONArray(cityString);
//Get the first and only JSONObject within each of the arrays.
JSONObject resultsJObject = resultsJArray.getJSONObject(0);
JSONObject cityJObject = cityJArray.getJSONObject(0);
//Get the required values from the JSONObject within city.
int city_id = cityJObject.getInt("id");
String city_name = cityJObject.getString("name");
String city_slug = cityJObject.getString("slug");
//Put the values into the results JSONObject.
resultsJObject.put("city_id", city_id);
resultsJObject.put("city_name", city_name);
resultsJObject.put("city_slug", city_slug);
//Print to verify.
System.out.println("Updated resultsJArray: " + resultsJArray);
}
catch (JSONException e)
{
e.printStackTrace();
}
public static void CombinTest(){
String a = "[{\"id\": 248, \n" +
"\"name\": \"Alternatif Cibubur\", \n" +
"\"slug\": \"alternatif-cibubur\", \n" +
"\"status\": \"active\", \n" +
"\"longitude\": 106.900786}]";
String b = "[{\"id\": 11, \n" +
"\"name\": \"Depok\", \n" +
"\"slug\": \"depok\"\n" +
"}]";
JSONArray resultObjArr = JSON.parseArray(a);
JSONArray cityObjArr = JSON.parseArray(b);
for (int i = 0; i < resultObjArr.size(); i++)
{
JSONObject city = cityObjArr.getJSONObject(i);
Set<String> keys = city.keySet();
if(keys.size()>0){
for(String key:keys){
resultObjArr.getJSONObject(i).put("city_"+key,city.get(key));
}
}
}
System.out.println(JSON.toJSONString(resultObjArr));
}