0

I have JSON response like below

{
    "XYZ": {
        "ABC": {
            "PQR": [
                {
                    "details": {
                        "date":1221,
                        "number": 30
                    }
                }
            ]
        }
    }
}

I want to fetch the details of "date" and "number" directly. without parsing separately XYZ, ABC,PQR AND details.

struct Trial: Mappable {
    var PQR!
    init() {}
    init?(map: Map) {}

    mutating func mapping(map: Map) {
        trialPeriod <- map["XYZ.ABC.PQR"]    
    }
}

I am to parse till PQR. After that, I am not able to parse. Could you please let me know how to get "details" after parsing till PQR??

OR

Let me know how to get the parse directly to a number? I tried many times but could not able to do so.

0

1 Answer 1

2

You couldn't parse nested after the PQR just because it is a JSON Array not a JSON Object. So, you can do something like that if you are sure that it is the first element of the array only.

struct Trial: Mappable {
    var PQR!
    init() {}
    init?(map: Map) {}

    mutating func mapping(map: Map) {
        trialPeriod <- map["XYZ.ABC.PQR.0.details.date"]
    }
}

Notice the .0, it means the first item in the array.

Or, the better, is trying to parse XYZ.ABC.PQR as an array, then parse individual items in it

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Buddy!! I was keep trying before posted like map["XYZ.ABC.PQR[0].details.date"] :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.