本文目錄一覽:
用java怎麼解析複雜的JSON
json-lib-2.4-jdk15.jar 這個包還另需一下幾個包。
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
把上邊的json定義為一個字元串 str,建議不要直接用還是自己動手寫寫。這樣比較容易掌握方法。
JSONObject ob=JSONObject.fromObject(str);
Object success=ob.get(“success”);
Object errorMsg=ob.get(“errorMsg”);
System.out.println(success);
System.out.println(errorMsg);
JSONObject data=ob.getJSONObject(“data”);
Object total=data.get(“total”);
System.out.println(total);
JSONArray array=data.getJSONArray(“rows”);
JSONObject rows=null;
for(int i=0;iarray.size();i++){
rows=array.getJSONObject(i);
Object id=rows.get(“id”);
System.out.println(id);
Object workName=rows.get(“workName”);
System.out.println(workName);
Object assigneeName=rows.get(“assigneeName”);
System.out.println(assigneeName);
Object name=rows.get(“name”);
System.out.println(name);
Object processInstanceInitiatorName=rows.get(“processInstanceInitiatorName”);
System.out.println(processInstanceInitiatorName);
Object processInstanceStartTime=rows.get(“processInstanceStartTime”);
System.out.println(processInstanceStartTime);
Object createTime=rows.get(“createTime”);
System.out.println(createTime);
Object dueDate=rows.get(“dueDate”);
System.out.println(dueDate);
}
java 如何解析JSON
一、JSON(JavaScriptObjectNotation)一種簡單的數據格式,比xml更輕巧。Json建構於兩種結構:1、「名稱/值」對的集合(Acollectionofname/valuepairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hashtable),有鍵列表(keyedlist),或者關聯數組(associativearray)。如:{「name」:」jackson」,「age」:100}2、值的有序列表(Anorderedlistofvalues)。在大部分語言中,它被理解為數組(array)如:{「students」:[{「name」:」jackson」,「age」:100},{「name」:」michael」,」age」:51}]}二、java解析JSON步驟A、伺服器端將數據轉換成json字元串首先、伺服器端項目要導入json的jar包和json所依賴的jar包至builtPath路徑下(這些可以到JSON-lib官網下載:)然後將數據轉為json字元串,核心函數是:publicstaticStringcreateJsonString(Stringkey,Objectvalue){JSONObjectjsonObject=newJSONObject();jsonObject.put(key,value);returnjsonObject.toString();}B、客戶端將json字元串轉換為相應的javaBean1、客戶端獲取json字元串(因為android項目中已經集成了json的jar包所以這裡無需導入)publicclassHttpUtil{publicstaticStringgetJsonContent(StringurlStr){try{//獲取HttpURLConnection連接對象URLurl=newURL(urlStr);HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();//設置連接屬性httpConn.setConnectTimeout(3000);httpConn.setDoInput(true);httpConn.setRequestMethod(“GET”);//獲取相應碼intrespCode=httpConn.getResponseCode();if(respCode==200){returnConvertStream2Json(httpConn.getInputStream());}}catch(MalformedURLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}return””;}privatestaticStringConvertStream2Json(InputStreaminputStream){StringjsonStr=””;//ByteArrayOutputStream相當於內存輸出流ByteArrayOutputStreamout=newByteArrayOutputStream();byte[]buffer=newbyte[1024];intlen=0;//將輸入流轉移到內存輸出流中try{while((len=inputStream.read(buffer,0,buffer.length))!=-1){out.write(buffer,0,len);}//將內存流轉換為字元串jsonStr=newString(out.toByteArray());}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnjsonStr;}}2、獲取javaBeanpublicstaticPersongetPerson(StringjsonStr){Personperson=newPerson();try{//將json字元串轉換為json對象JSONObjectjsonObj=newJSONObject(jsonStr);//得到指定jsonkey對象的value對象JSONObjectpersonObj=jsonObj.getJSONObject(“person”);//獲取之對象的所有屬性person.setId(personObj.getInt(“id”));person.setName(personObj.getString(“name”));person.setAddress(personObj.getString(“address”));}catch(JSONExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnperson;}publicstaticListgetPersons(StringjsonStr){Listlist=newArrayList();JSONObjectjsonObj;try{//將json字元串轉換為json對象jsonObj=newJSONObject(jsonStr);//得到指定jsonkey對象的value對象JSONArraypersonList=jsonObj.getJSONArray(“persons”);//遍歷jsonArrayfor(inti=0;i
Java json多層解析
JSONObject的dataJson =新的JSONObject(「你的JSON數據」);
JSONObject的響應= dataJson.getJSONObject(「響應」);
JSONArray數據= response.getJSONArray(「數據」); JSONObject的信息= data.getJSONObject(0);
字元串省= info.getString(「省」);
弦樂城市= info.getString(「城市」);
字元串區= info.getString(「區」);
字元串地址= info.getString(「地址」);
System.out.println(省+市+區+地址);
java怎麼解析我這個json數據
一、 JSON (JavaScript Object Notation)一種簡單的數據格式,比xml更輕巧。
Json建構於兩種結構:
1、「名稱/值」對的集合(A collection of name/value pairs)。不同的語言中,它被理解為對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。 如:
{
「name」:」jackson」,
「age」:100
}
2、值的有序列表(An ordered list of values)。在大部分語言中,它被理解為數組(array)如:
{
「students」:
[
{「name」:」jackson」,「age」:100},
{「name」:」michael」,」age」:51}
]
}
二、java解析JSON步驟
A、伺服器端將數據轉換成json字元串
首先、伺服器端項目要導入json的jar包和json所依賴的jar包至builtPath路徑下(這些可以到JSON-lib官網下載:)
然後將數據轉為json字元串,核心函數是:
public static String createJsonString(String key, Object value)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
}
原創文章,作者:V74RE,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127376.html