如何组合两个JSONArrays JAVA(下面的特殊要求)

时间:2017-05-09 02:27:37

标签: java arrays json

我有一个代码片段将两个JSONArrays合并为一个。在这里,

for (i = 0; i < cityJArray.length(); i++) 
{
    resultsJArray.put(cityJArray.getJSONObject(i));
}

它将第二个JSONArray(cityJArray)添加到第一个(resultsJArray)。但是,我的要求是将第二个添加到第一个JSONArray的第一个数组中(我不能用语言解释,但我会尝试解释使用代码)。

这是JSONObject:

{"results": 
[{"id": 248, 
"name": "Alternatif Cibubur", 
"slug": "alternatif-cibubur", 
"status": "active", 
"city": 
    {"id": 11, 
    "name": "Depok", 
    "slug": "depok",
    }, 
"longitude": 106.900786}]}

我已将它转换为JSONArray,因为我必须将字段&#39; city&#39;并将其属性名称更改为&#39; city_id&#39;,&#39; city_name&#39;,&#39; city_slug&#39;。因为他们会创建副本,因为它已经是&#39;结果&#39; field(参见给定的JSONObject)。

(目的:从JSONObject创建.CSV文件)

(问题开始:当我尝试从JSONObject中分离一个字段以使其进入不同的列时)

JSONArrays详细信息:

resultsJArray:

[{"id": 248, 
"name": "Alternatif Cibubur", 
"slug": "alternatif-cibubur", 
"status": "active",  
"longitude": 106.900786}]

cityJArray:

[{"id": 11, 
"name": "Depok", 
"slug": "depok"
}]

我想将这两个单独的JSONArrays添加到这样的(下面是预期的输出)

[{"id": 248, 
"name": "Alternatif Cibubur", 
"slug": "alternatif-cibubur", 
"status": "active",
"longitude": 106.900786,
"city_id": 11, 
"city_name": "Depok", 
"city_slug": "depok"}]

2 个答案:

答案 0 :(得分:0)

这应该按照要求工作,我已经注意到两个数组最初都是字符串,然后转换为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();
}

答案 1 :(得分:0)

我使用“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));
        }