简体   繁体   中英

How to retrieve the values from json

I am converting xml message into json object and i want to read all the values from json object how can i read all the values Example-pfirstname="Anssi"; like this i want to read all the values in the json formated string. I am using jsonobject to convert

JSONObject xmlJSONObj = XML.toJSONObject(xml1);

{
"event": {
    "eventpermits": {
        "eventpermit": {
            "pfirstname": "Anssi",
            "pmodifydate": {
                "minute": 20,
                "second": 40,
                "month": 6,
                "year": 2012,
                "hour": 11,
                "day": 21
            },
            "pplates": "ABC123",
            "genddate": {
                "minute": 59,
                "second": 59,
                "month": 12,
                "year": 9999,
                "hour": 23,
                "day": 31
            },
            "gname": "Visy",
            "permitreason": 2,
            "penddate": {
                "minute": 0,
                "second": 0,
                "month": 6,
                "year": 2012,
                "hour": 17,
                "day": 21
            },
            "accesscheckdate": {
                "minute": 20,
                "second": 45,
                "month": 6,
                "year": 2012,
                "hour": 11,
                "day": 21
            },
            "pstartdate": {
                "minute": 0,
                "second": 0,
                "month": 6,
                "year": 2012,
                "hour": 12,
                "day": 20
            },
            "id": 13,
            "selected": 1,
            "plastname": "Huttunen",
            "gstartdate": {
                "minute": 0,
                "second": 59,
                "month": 6,
                "year": 2012,
                "hour": 0,
                "day": 20
            },
            "points": 1,
            "eventnumber": 4,
            "gaccesslevel": 0,
            "accesscheckid": 17,
            "pid": 4,
            "pcreatorname": "VisyXMLGate",
            "pcpallowed": 1,
            "gcheckpointallowed": 1,
            "gid": 1,
            "pdeletedate": {
                "minute": 0,
                "second": 0,
                "month": 6,
                "year": 2012,
                "hour": 0,
                "day": 21
            },
            "povt": "X2",
            "pcompany": "Visy",
            "pcreationdate": {
                "minute": 19,
                "second": 27,
                "month": 6,
                "year": 2012,
                "hour": 11,
                "day": 21
            }
        }
    },
    "accessdecision": {
        "cppolicy": "OPENFORPERMIT",
        "id": 17,
        "permitreason": 2,
        "date": {
            "minute": 20,
            "second": 45,
            "month": 6,
            "year": 2012,
            "hour": 11,
            "day": 21
        },
        "eventnumber": 4,
        "frontplate": "ABC123"
    },
    "permitreason": 2,
    "checkpoint": {
        "messagedirection": "IN",
        "id": 1,
        "direction": "IN",
        "tag": "IN1",
        "name": "Entrance 1",
        "policy": "OPENFORPERMIT",
        "sourceareaid": 1,
        "targetareaid": 2
    },
    "permitid": 4,
    "frontlicenseplates": {
        "licenseplate": {
            "formatted": "ABC123",
            "nationality": "FIN",
            "unformatted": "",
            "confidence": 1.1
        }
    },
    "date": {
        "minute": 18,
        "second": 29,
        "month": 6,
        "year": 2012,
        "hour": 11,
        "day": 21
    },
    "eventnumber": 4,
    "ovt": "VisyAccessGate"
}
}

According to your Json string structure, all are formatted with JSONObject {} and no JSONArray [] containing.

Here is example to get pfirstname value, and like this way, you can try other values yourself.

//firstly get 'event' JSONObject
JSONObject event = xmlJSONObj.getJSONObject("event");

//from 'event' JSONObject, get 'eventpermits' JSONObject as it is enclosing with {}
JSONObject eventpermits = event.getJSONObject("eventpermits");

//to get 'eventpermit' JSONObject
JSONObject eventpermit = eventpermits.getJSONObject("eventpermit");

//to get JSONObject of 'pmodifydate' AND one string value of 'pfirstname'
JSONObject pmodifydate = eventpermit.getJSONObject("pmodifydate");

//string value of 'pfirstname'
String pfirstname = eventpermit.getString("pfirstname");

When you access value with its type, according to your JSON string structure, you can use the right accessor with field name -

  • getString()

  • getBoolean()

  • getInt(), getLong(), getDouble()

See api here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM