我有两个JSON,我想合并为一个。
JSON 1
[
{
"city":"Aberdeen",
"year":1996,
"occupancy_change":null,
"effective_rent_change":null
},
{
"city":"Aberdeen",
"year":1997,
"occupancy_change":null,
"effective_rent_change":null
},
{
"city":"Ambler",
"year":1996,
"occupancy_change":1.65,
"effective_rent_change":4.37
},
{
"city":"Ambler",
"year":1997,
"occupancy_change":1.17,
"effective_rent_change":-0.25
}
]
JSON 2
[
{
"year":1996,
"pma_occupancy_change":null,
"pma_effective_rent_change":null
},
{
"year":1997,
"pma_occupancy_change":null,
"pma_effective_rent_change":null
}
]
我想将两个json合并为一个,我想使用以下格式。
[
{
"year":1996,
"pma_occupancy_change":null,
"pma_effective_rent_change":null,
"city":"Aberdeen",
"occupancy_change":null,
"effective_rent_change":null,
"city":"Ambler",
"occupancy_change":1.65,
"effective_rent_change":4.37
},
{
"year":1997,
"pma_occupancy_change":null,
"pma_effective_rent_change":null,
"city":"Aberdeen",
"occupancy_change":null,
"effective_rent_change":null
"city":"Ambler",
"occupancy_change":1.17,
"effective_rent_change":-0.25
}
]
到目前为止,这是我尝试过的方法,但对我而言无效。
from collections import defaultdict
null = None
d = defaultdict(dict)
for l in (city_li, pma_li):
for elem in l:
d[elem['year']].update(elem)
l3 = d.values()
print l3
答案 0 :(得分:0)
public void concat2JSONStrings() {
String JSONString1 = "[ \r\n" +
" { \r\n" +
" \"city\":\"Aberdeen\",\r\n" +
" \"year\":1996,\r\n" +
" \"occupancy_change\":null,\r\n" +
" \"effective_rent_change\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \"city\":\"Aberdeen\",\r\n" +
" \"year\":1997,\r\n" +
" \"occupancy_change\":null,\r\n" +
" \"effective_rent_change\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \"city\":\"Ambler\",\r\n" +
" \"year\":1996,\r\n" +
" \"occupancy_change\":1.65,\r\n" +
" \"effective_rent_change\":4.37\r\n" +
" },\r\n" +
" { \r\n" +
" \"city\":\"Ambler\",\r\n" +
" \"year\":1997,\r\n" +
" \"occupancy_change\":1.17,\r\n" +
" \"effective_rent_change\":-0.25\r\n" +
" }\r\n" +
"]";
String JSONString2 = "[ \r\n" +
" { \r\n" +
" \"year\":1996,\r\n" +
" \"pma_occupancy_change\":null,\r\n" +
" \"pma_effective_rent_change\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \"year\":1997,\r\n" +
" \"pma_occupancy_change\":null,\r\n" +
" \"pma_effective_rent_change\":null\r\n" +
" }\r\n" +
"]";
JsonPath jp = new JsonPath(JSONString1);
JsonPath jp1 = new JsonPath(JSONString2);
System.out.println(jp.get("city[0]").toString());
System.out.println(jp1.get("year[0]").toString());
JSONObject json = new JSONObject();
json.put("year", jp.get("city[0]"));
json.put("pma_occupancy_change", jp1.get("pma_occupancy_change[0]"));
Once you have the JSONObject then you can convert to JSONArray and meet your output.
答案 1 :(得分:0)
使用简单JSON的Java解决方案:
@Test
public void concat2JSONStrings() {
String JSONString1 = "[ \r\n" +
" { \r\n" +
" \"city\":\"Aberdeen\",\r\n" +
" \"year\":1996,\r\n" +
" \"occupancy_change\":null,\r\n" +
" \"effective_rent_change\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \"city\":\"Aberdeen\",\r\n" +
" \"year\":1997,\r\n" +
" \"occupancy_change\":null,\r\n" +
" \"effective_rent_change\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \"city\":\"Ambler\",\r\n" +
" \"year\":1996,\r\n" +
" \"occupancy_change\":1.65,\r\n" +
" \"effective_rent_change\":4.37\r\n" +
" },\r\n" +
" { \r\n" +
" \"city\":\"Ambler\",\r\n" +
" \"year\":1997,\r\n" +
" \"occupancy_change\":1.17,\r\n" +
" \"effective_rent_change\":-0.25\r\n" +
" }\r\n" +
"]";
String JSONString2 = "[ \r\n" +
" { \r\n" +
" \"year\":1996,\r\n" +
" \"pma_occupancy_change\":null,\r\n" +
" \"pma_effective_rent_change\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \"year\":1997,\r\n" +
" \"pma_occupancy_change\":null,\r\n" +
" \"pma_effective_rent_change\":null\r\n" +
" }\r\n" +
"]";
JsonPath jp = new JsonPath(JSONString1);
JsonPath jp1 = new JsonPath(JSONString2);
JSONObject json = new JSONObject();
json.put("year", jp.get("city[0]"));
json.put("pma_occupancy_change", jp1.get("pma_occupancy_change[0]"));
json.put("pma_effective_rent_change", jp1.get("pma_effective_rent_change[0]"));
json.put("city",jp.get("city[0]"));
json.put("occupancy_change",jp.get("occupancy_change[0]"));
json.put("effective_rent_change",jp.get("effective_rent_change[0]"));
json.put("city",jp.get("city[2]"));
json.put("occupancy_change",jp.get("occupancy_change[2]"));
json.put("effective_rent_change",jp.get("effective_rent_change[2]"));
JSONObject json1 = new JSONObject();
json1.put("year", jp.get("city[2]"));
json1.put("pma_occupancy_change", jp1.get("pma_occupancy_change[0]"));
json1.put("pma_effective_rent_change", jp1.get("pma_effective_rent_change[0]"));
json1.put("city",jp.get("city[0]"));
json1.put("occupancy_change",jp.get("occupancy_change[0]"));
json1.put("effective_rent_change",jp.get("effective_rent_change[0]"));
json1.put("city",jp.get("city[2]"));
json1.put("occupancy_change",jp.get("occupancy_change[3]"));
json1.put("effective_rent_change",jp.get("effective_rent_change[3]"));
JSONArray ja1 = new JSONArray();
ja1.add(json);
ja1.add(json1);
System.out.println("Required appended JSON Output is: "+ja1.toJSONString());
}
所需的输出:
必需的附加JSON输出为:
[{“ effective_rent_change”:4.37,“ year”:“ Aberdeen”,“ city”:“ Ambler”,“ pma_effective_rent_change”:null,“ pma_occupancy_change”:null,“ occupancy_change”:1.65},{“ effective_rent_change” :-0.25,“ year”:“ Ambler”,“ city”:“ Ambler”,“ pma_effective_rent_change”:null,“ pma_occupancy_change”:null,“ occupancy_change”:1.17}]
答案 2 :(得分:-2)
合并两个字符串。
@Test
public void concat2JSONStrings() {
String JSONString1 = "\"[ \r\n" +
" { \r\n" +
" \\\"city\\\":\\\"Aberdeen\\\",\r\n" +
" \\\"year\\\":1996,\r\n" +
" \\\"occupancy_change\\\":null,\r\n" +
" \\\"effective_rent_change\\\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \\\"city\\\":\\\"Aberdeen\\\",\r\n" +
" \\\"year\\\":1997,\r\n" +
" \\\"occupancy_change\\\":null,\r\n" +
" \\\"effective_rent_change\\\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \\\"city\\\":\\\"Ambler\\\",\r\n" +
" \\\"year\\\":1996,\r\n" +
" \\\"occupancy_change\\\":1.65,\r\n" +
" \\\"effective_rent_change\\\":4.37\r\n" +
" },\r\n" +
" { \r\n" +
" \\\"city\\\":\\\"Ambler\\\",\r\n" +
" \\\"year\\\":1997,\r\n" +
" \\\"occupancy_change\\\":1.17,\r\n" +
" \\\"effective_rent_change\\\":-0.25\r\n" +
" }\r\n" +
"]\"";
String JSONString2 = "\"[ \r\n" +
" { \r\n" +
" \\\"year\\\":1996,\r\n" +
" \\\"pma_occupancy_change\\\":null,\r\n" +
" \\\"pma_effective_rent_change\\\":null\r\n" +
" },\r\n" +
" { \r\n" +
" \\\"year\\\":1997,\r\n" +
" \\\"pma_occupancy_change\\\":null,\r\n" +
" \\\"pma_effective_rent_change\\\":null\r\n" +
" }\r\n" +
"]\"";
System.out.println(JSONString1.concat(JSONString2));
}
输出:
"[
{
\"city\":\"Aberdeen\",
\"year\":1996,
\"occupancy_change\":null,
\"effective_rent_change\":null
},
{
\"city\":\"Aberdeen\",
\"year\":1997,
\"occupancy_change\":null,
\"effective_rent_change\":null
},
{
\"city\":\"Ambler\",
\"year\":1996,
\"occupancy_change\":1.65,
\"effective_rent_change\":4.37
},
{
\"city\":\"Ambler\",
\"year\":1997,
\"occupancy_change\":1.17,
\"effective_rent_change\":-0.25
}
]""[
{
\"year\":1996,
\"pma_occupancy_change\":null,
\"pma_effective_rent_change\":null
},
{
\"year\":1997,
\"pma_occupancy_change\":null,
\"pma_effective_rent_change\":null
}
]"