2015年6月9日 星期二
【Android】Json 的建立
建立範例
{
"type": "2",
"body": {
"description": [
{
"dns1": "140.113.17.5",
"dns2": "140.113.1.1",
"static": "1",
"gateway": "140.113.17.254",
"netmask": "255.255.255.0",
"ip": "140.113.17.178"
},
{
"dhcp": "0"
},
{
"pppoe": "0"
}
]
},
"deviceId": "BW-9FEFB676A5DC556C18D3D24D9FC334C4"
}
建立function
/**
* setWanInfoJson
* @param responseJson
*/
public String setWanInfoJson(Context ctx, String deviceId) {
JSONObject sendJson = new JSONObject();
try {
sendJson.put("type", "2");
sendJson.put("deviceId", deviceId);
// 構造三個JSONObject 、一個static, dhcp 和 pppoe物件
JSONObject staticJson = new JSONObject();
staticJson.put("static", "1");
staticJson.put("ip", "140.113.17.178");
staticJson.put("netmask", "255.255.255.0");
staticJson.put("gateway", "140.113.17.254");
staticJson.put("dns1", "140.113.17.5");
staticJson.put("dns2", "140.113.1.1");
JSONObject dhcpJson = new JSONObject();
dhcpJson.put("dhcp", "0");
JSONObject pppoeJson = new JSONObject();
pppoeJson.put("pppoe", "0");
// We add the object to the description Array
JSONArray descriptionArr = new JSONArray();
descriptionArr.put(staticJson);
descriptionArr.put(dhcpJson);
descriptionArr.put(pppoeJson);
// We add the descriptionArr to the body object
JSONObject body = new JSONObject();
body.put("description", descriptionArr);
// We add the object to the main object
sendJson.put("body", body);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("sendJson:" + sendJson.toString());
return sendJson.toString();
}
忘記某些資訊, 可參考
http://stackoverflow.com/questions/18983185/how-to-create-correct-jsonarray-in-java-using-jsonobject
https://code.google.com/p/json-simple/wiki/DecodingExamples#Example_1_-_Convenient_way:_Use_JSONValue
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言