java中字符串怎么转json?

JavaScript019

java中字符串怎么转json?,第1张

string类型如果要转换成json的话,就需要写成这样的形式,如下:\x0d\x0aString jsonStr ="{'id':'11','parentId':'root','refObj':{'existType':'exist','deptType':'emp','treeNodeType':'dept'}}"\x0d\x0aJSONObject jsonObj = new JSONObject(jsonStr)\x0d\x0aJSONObject refObj = new JSONObject(jsonObj.getString("refObj"))\x0d\x0aString existType = refObj.getString("existType")\x0d\x0aSystem.out.println(existType)\x0d\x0ajar使用的是org.json.jar

jQuery中使用jQuery.parseJSON()方法将字符串转为json格式 例如: jQuery.parseJSON('{"test":1}')注意: 传入格式有误的 JSON 字符串可能导致抛出异常。

例如:JSON字符串:varstr1='{"name":"cxh","sex":"man"}'JSON对象:varstr2={"name":"cxh","sex":"man"}一、JSON字符串转换为JSON对象要使用上面的str1,必须使用下面的方法先转化为JSON对象://由JSON字符串转换为JSON