JSON 解析獲取到具體的得屬性得值


獲取某JSON值

    String json = "[{\"OnLineGap\":\"60000\"}]";  

    JSONArray array = JSONArray.fromObject(json);  

    JSONObject jsonObject = array.getJSONObject(0);  

     System.out.println(jsonObject.get("OnLineGap"));  //獲取到值

 

json數據格式如下格式,獲取到具體的值並且獲取到 ‘score’得最大值,詳細看下。

 

public Response faceVerify(HttpServletRequest request, String json) {
        Response response = new Response();
        if(StringUtils.isBlank ( json )){
            response.setError ( "JSON參數不可以為空!" );
            response.setSuccess ( false );
            return  response;
        }
        JSONObject jsonObject_ = JSONObject.fromObject(json);
        List<Double> listScore = new ArrayList<Double>();
        String person_id = "";
        String face_id = "";
        JSONObject ListObject ;
        JSONArray jsonArray ;
        Double maxScores;
        try {
            jsonArray = JSONArray.fromObject ( jsonObject_.get ( "list" ));
            for (int i = 0; i < jsonArray.size(); i++) {
                ListObject = JSONObject.fromObject (jsonArray.get ( i ));
                //找到“score”屬性得每一個值
                listScore.add((Double) ListObject.get("score"));
            }
            maxScores = Collections.max(listScore);
            if (maxScores != null) {
                for (int i2 = 0; i2 < jsonArray.size(); i2++) {
                    JSONObject jsonObjects = jsonArray.getJSONObject(i2);
                    if ((Double) jsonObjects.get("score") == maxScores) {
                        //獲取到 ’person_id‘ 屬性的值
                        person_id += jsonObjects.get("person_id").toString();
                        face_id += jsonObjects.get("face_id").toString();
                        if (StringUtils.isEmpty(person_id) && StringUtils.isEmpty(face_id)) {
                            response.setSuccess(false);
                            response.setError("數據異常,數據不可以為空!");
                            return response;
                        }
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.getMessage();
            response.setError("數據格式非法,解析失敗!");
            response.setSuccess(false);
            return response;
        }
        dao.faceVerify(request, person_id, face_id, maxScores);
        return response;
    }

 

String a="{"code":100,"data":{"grdbl":100.0,"bxl":646,"fwl":0,"mytsl":0}}";
 
 JSONObject object=JSONObject.fromObject(a);
System.out.println(object.getJSONObject("data").get("grdbl"));
System.out.println(object.getJSONObject("data").get("bxl"));
System.out.println(object.getJSONObject("data").get("fwl"));
System.out.println(object.getJSONObject("data").get("mytsl"));

 

//************************************************************************************************************************************************

3.1基本的JSONArray與JSONObject操作

復制代碼
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class ObjectAndArray1 { public static void main(String args[]) { JSONObject jsonObj = new JSONObject(); jsonObj.put("name0", "zhangsan"); jsonObj.put("sex1", "female"); System.out.println(jsonObj); //輸出為:{"sex1":"female","name0":"zhangsan"}  JSONArray jsonArray = new JSONArray(); jsonArray.add("11"); jsonArray.add("22"); jsonArray.add("33"); System.out.println(jsonArray); //輸出為:["11","22","33"]  } }
復制代碼

3.2由java自帶的數據結構轉換為JSON文本

復制代碼
import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class ObjectAndArray2{ public static void main(String args[]) { //可以由數組,列表等生成JSONArray String list[]={"11","22"}; JSONArray jsonarray = JSONArray.fromObject(list); jsonarray.add("33"); System.out.println(jsonarray); //輸出為:["11","22","33"] //可以由Map生成JSONObject Map<String,Object> map=new HashMap<String,Object>(); map.put("NO1", "第一個"); map.put("NO2", "第二個"); map.put("NO3", jsonarray); JSONObject jsonObj = JSONObject.fromObject(map); System.out.println(jsonObj); //輸出為:{"NO3":["11","22","33"],"NO2":"第二個","NO1":"第一個"}  } }
復制代碼

3.3讀取JSON文本

JSONArray必須用下標讀取內部數據。
JSONObject必須用”鍵“讀取對應的”值“。
復制代碼
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class ObjectAndArray { public static void main(String args[]) { JSONArray jsonarray; JSONObject jsonObj; //讀取JSONArray,用下標索引獲取 String array="[\"11\",\"22\",\"33\"]"; jsonarray = JSONArray.fromObject(array); System.out.println(jsonarray.getString(1)); //輸出為:22 //讀取JSONObject String object="{\"NO1\":[\"44\",\"55\",\"66\"],\"NO2\":{\"NO1\":\"第一個\"}}"; jsonObj = JSONObject.fromObject(object); System.out.println(jsonObj.get("NO1")); //輸出為:["44","55","66"]  jsonarray = (JSONArray)(jsonObj.get("NO1")); System.out.println(jsonarray.getString(1)); //輸出為:55 //用"鍵"獲取值 jsonObj=(JSONObject)jsonObj.get("NO2"); System.out.println(jsonObj); //輸出為:{"NO1":"第一個"}  } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM