JSON学习2
- 生成JSON
- 解析JSON
java解析json字符串和合成json字符串
json字符串
{"type":"getConfig","ip":"192.168.1.100"}
使用
String ss= groupJS("Config","192.168.1.100");
splitJS(ss);
回显
I/lxh: groupJS: js={"type":"Config","ip":"192.168.1.100"}
D/lxh: splitJS: type=Config,ip=192.168.1.100
生成JSON
public String groupJS(String mtype, String mip) {String JS = "";JSONObject groupJSON = new JSONObject();try {groupJSON.put("type", mtype);groupJSON.put("ip", mip);} catch (JSONException e) {e.printStackTrace();}JS = groupJSON.toString();Log.i("lxh", "groupJS: js=" + JS);return JS;}
解析JSON
方法一:
public void splitJS(String ss) {JSONObject readMag = null;try {readMag = new JSONObject(ss);String mtype =readMag.getString("type");String mip =readMag.getString("ip");Log.d("lxh", "splitJS: type=" + mtype + ",ip=" + mip);} catch (JSONException e) {e.printStackTrace();}}
未完待续。。。
与君共勉!待续
欢迎指错,一起学习