JSONObject与json字符串彼此转化
wangxl@20180718
所需jar包
commons-beanutils-1.8.0.jar不加这个包
java.lang.NoClassDefFoundError:org/apache/commons/beanutils/DynaBean
2、commons-collections-3.1.jar不加这个包
java.lang.NoClassDefFoundError:org/apache/commons/collections/map/ListOrderedMap
3、commons-lang-2.5.jar不加这个包
java.lang.NoClassDefFoundError:org/apache/commons/lang/exception/NestableRuntimeException
4、commons-logging-1.2.jar不加这个包
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
5、ezmorph-1.0.6.jar不加这个包
java.lang.NoClassDefFoundError:net/sf/ezmorph/Morpher
6、json-lib-2.4-jdk15.jar不加这个包
java.lang.NoClassDefFoundError:net/sf/json/JSONObject
参阅下载网址:https://download.csdn.net/download/flyme2010/10548102
函数参阅
//json字符串转成JSONObject
publicvoidJsonStr2JSONObject()
{undefined
Stringjsonstr=”{”msgType”:”1”,”hasMore”:”0”,”data”:[{”serviceId”:”StreetLight”,”serviceData”:{”length”:12,”value”:”123456789012”}}]}”;
JSONObjectjsonObject=JSONObject.fromObject(jsonstr);
StringmsgType=jsonObject.getString(“msgType”);
System.out.println(“msgType=”+msgType);//输出:msgType=1
JSONArraydata=jsonObject.getJSONArray(“data”);
Stringss=data.getString(0);
JSONObjectdata2=JSONObject.fromObject(ss);
StringserviceId=data2.getString(“serviceId”);
System.out.println(“serviceId=”+serviceId);//输出:serviceId=StreetLight
StringserviceData=data2.getString(“serviceData”);
JSONObjectdata3=JSONObject.fromObject(serviceData);
Stringvalue=data3.getString(“value”);
System.out.println(“value=”+value);//输出:value=123456789012
}
//JSONObject生成json字符串
publicvoidJSONObject2JsonStr()
{undefined
JSONObjectjsonObject=newJSONObject();
MapparamData=newHashMap<>();
MapserviceData=newHashMap<>();
serviceData.put(“length”,newInteger(12));
serviceData.put(“value”,”123456789012″);
paramData.put(“serviceId”,”StreetLight”);
paramData.put(“serviceData”,serviceData);
List>cmds=newArrayList>();//数组
cmds.add(paramData);
jsonObject.put(“msgType”,”1″);
jsonObject.put(“hasMore”,newInteger(0));
jsonObject.put(“data”,cmds);
System.out.println(jsonObject.toString());
//打印输出:{“msgType”:”1″,”hasMore”:0,”data”:[{“serviceData”:{“length”:12,”value”:”123456789012″},”serviceId”:”StreetLight”}]}
}

js

string字符串转json

以下均利用Gson来处理:
1.将bean转换成Json字符串:
publicstaticStringbeanToJSONString(Objectbean){
returnnewGson().toJson(bean);
}
2.将Json字符串转换成对象:
publicstaticObjectJSONToObject(Stringjson,ClassbeanClass){
Gsongson=newGson();
Objectres=gson.fromJson(json,beanClass);
returnres;
}
注:转换后的对象使用前进行强转:例如beanbean1=(bean)FormatUtil.JSONToObject(json,bean.class);
3.将Json字符串转换成JsonObject对象:
1
JsonObjectreturnData=newJsonParser().parse(jsonstr).getAsJsonObject();
4.将JsonArray类型的Json字符串解析成对象方法:
Gsongson=newGson();
JsonParserparser=newJsonParser();
JsonArrayJarray=parser.parse(jstring).getAsJsonArray();
ArrayList<channelSearchEnum>lcs=newArrayList<channelSearchEnum>();
for(JsonElementobj:Jarray){
channelSearchEnumcse=gson.fromJson(obj,channelSearchEnum.class);
lcs.add(cse);
}
或者
Stringjson=”[{”username”:”test”},{”username”:”test2”}]”;
System.out.println(newJsonParser().parse(json).getAsJsonArray().get(0).getAsJsonObject().get(“username”).getAsString());
5.获取JsonObject中指定key值对应的JsonArray对象:
Stringjson=”{”pids”:[”1”,”2”,”3”]}”;
System.out.println(newJsonParser().parse(json).getAsJsonObject().getAsJsonArray(“pids”).get(0).getAsString());

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。