JAVA解析Json数据

Posted Talk is cheap. Show me the cod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA解析Json数据相关的知识,希望对你有一定的参考价值。

JSON数据格式:

{"EventType": "event_user_create","UserId": ["123", "124", "125"],"DepId":["depId"],"CorpId": "corpid"}

JAVA解析:

public static void main(String[] args) {
    String aaa="{"EventType": "event_user_create","UserId": ["123", "124", "125"],"DepId":["depId"],"CorpId": "corpid"}";
    JSONObject object=JSONObject.fromObject(aaa);
    System.out.println(object);
    if(object.size()>0){
            Object EventType = object.get("EventType");
            System.out.println("EventType:"+EventType);
            JSONArray UserId = object.getJSONArray("UserId");
            for(int i=0;i<UserId.size();i++){
                Object object2 = UserId.get(i);
                System.out.println("UserId:"+object2);
            }
            System.out.println("UserId:"+UserId);
            JSONArray DepId = object.getJSONArray("DepId");
            System.out.println("DepId:"+DepId);
            Object CorpId = object.get("CorpId");
            System.out.println("CorpId:"+CorpId);
        }
}

结果:

{"EventType":"event_user_create","UserId":["123","124","125"],"DepId":["depId"],"CorpId":"corpid"}
EventType:event_user_create
UserId:123
UserId:124
UserId:125
UserId:["123","124","125"]
DepId:["depId"]
CorpId:corpid

 

以上是关于JAVA解析Json数据的主要内容,如果未能解决你的问题,请参考以下文章

java与JSON

java中把json怎么转换成数组

片段中的 JSON 解析 [关闭]

片段(Java) | 机试题+算法思路+考点+代码解析 2023

怎么用java解析xml中entity

java 解析json字符串