Java String string is converted into json array and traversed

If you need to convert the String string into a json array, and as long as the value of a field in the string, the field is a string of the json array, such as the following json form

[javascript]  view plain copy  
  1. {  
  2. returnCode: "return code" ,  
  3. returnMessage: "Returned error message" ,  
  4. rowCount: "Number of rows returned" ,  
  5. colCount: "Return the number of data columns" ,  
  6. requestParams: "call parameters" ,  
  7. requestTime: "receive request time" ,  
  8. responseTime: "Return request result time" ,  
  9. takeTime: "Time (in seconds)" ,  
  10.   DS:[   
  11.         {field name 1: "field value 1", field name 2: "field value 2", field name 3: "field value 3", ... },  
  12.         {field name 1: "field value 1", field name 2: "field value 2", field name 3: "field value 3", ... },  
  13.         {field name 1: "field value 1", field name 2: "field value 2", field name 3: "field value 3", ... },  
  14.         {field name 1: "field value 1", field name 2: "field value 2", field name 3: "field value 3", ... },  
  15.         {field name 1: "field value 1", field name 2: "field value 2", field name 3: "field value 3", ... }  
  16.     ]  
  17. }  


 
 

Get the value of the string DS and implement the code:

[java]  view plain copy  
  1. JSONObject jsonObject = JSONObject.fromObject(str); //String to json object  
  2. String data = jsonObject.getString( "DS" ); //Get DS content  
  3. JSONArray jsonArray = JSONArray.fromObject(data); //And take out the DS content and convert it to a json array  
  4. for  ( int  i =  0 ; i < jsonArray.size(); i++) {      //traverse the contents of the json array  
  5.     JSONObject object = jsonArray.getJSONObject(i);  
  6.     System.out.println(object.getString("字段名1"));  
  7. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324821426&siteId=291194637