简体   繁体   中英

Json Array Parsing issue

``I am having an Json Array as response like:

    [
        {
            "status": "Active",
            "entityName": "fghgfhfghfgh",
            "entityCode": 14,
            "children": [],
            "attributes": [
                {
                    "attributeValue": "500 michigan ave"
                }
            ],
            "deviceList": [],
            "entityId": "64eab9299eed9455b3683da074cf175c",
            "customerId": 2006546,
            "type": "7dad308f82b41e02fe8959c05b631bd7"
        }
,
        {
            "status": "Active",
            "entityName": "ghghhguyutgh6re58rrt",
            "entityCode": 13,
            "children": [],
            "attributes": [
                {
                    "attributeValue": "500 michigan ave"
                }
            ],
            "deviceList": [],
            "entityId": "912eff0613fa140c100af435c033e195",
            "customerId": 2006546,
            "type": "7dad308f82b41e02fe8959c05b631bd7"
        }
    ]

I want to split this json into two like

{ "status": "Active", "entityName": "fghgfhfghfgh", "entityCode": 14, "children": [], "attributes": [ { "attributeValue": "500 michigan ave" } ], "deviceList": [], "entityId": "64eab9299eed9455b3683da074cf175c", "customerId": 2006546, "type": "7dad308f82b41e02fe8959c05b631bd7" }

and the other one.I am using GSON and simplejson,when I try to remove the delimiters([ and ])the json comes as malformed one.Is there any better otpion to split the json array to two or more json strings as per the json response coming.

Is there any reason you can't parse the entire array, and then iterate over/process each element individually?

Just removing the brackets does make the JSON invalid, and splitting at the comma is going to be unreliable. The parser exists to figure out where to split the array and turn each element into an object for you.

Assuming you have some data structure defined to hold a single element once you've broken the array down, you should be able to parse the array into a list of those and step through them (or pick one out) as needed.

After that point, you can do whatever you want with the data (including formatting it back into JSON). I would definitely recommend using a proper parser to break the array down, though; it will be a lot simpler and more reliable, and should work unless you have serious performance concerns.

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