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":"第一个"}
        
    }
}

 

posted on 2019-06-06 15:51  UnmatchedSelf  阅读(1677)  评论(0编辑  收藏  举报

导航