这里是JSON字符串我所期待的:将两个字符串映射成JSON
{
"startDate": "2013-01-01",
"columns": "mode , event",
"endDate": "2013-02-01",
"selection": {
"selectionMatch": "123456789012",
"selectionType": "smart"
}
}
这里是Java代码,但我没能成功:
public static String BuildJson() throws JSONException{
Map<String, String> map1 = new HashMap<String, String>();
map1.put("startDate", "2013-01-01");
map1.put("endDate", "2013-02-01");
map1.put("columns", "mode , event");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("selectionType", "smart");
map2.put("selectionMatch", "123456789012");
JSONArray ja2 = new JSONArray();
ja2.put(map2);
System.out.println(ja2.toString());
map1.put("selection", ja2.toString());
System.out.println();
JSONArray ja = new JSONArray();
ja.put(map1);
System.out.println(ja.toString());
return null;
}
的挑战是如何组合两个不在同一级别的地图字符串。我的代码结果是:
[{"startDate":"2013-01-01","columns":"mode , event","endDate":"2013-02-01","selection":"[{\"selectionMatch\":\"123456789012\",\"selectionType\":\"smart\"}]"}]
有人可以帮我吗?
它的工作原理!非常感谢你,我花了很多时间。我对JSONObject不熟悉,所以现在你给我正确的方向,我要去浏览api。 – Eric
如果有效,请标记为正确答案。 – Selvaraj
完成,标记!谢谢 – Eric