1

I am trying to parse a JSON response in flutter but, there is a random "200," in the file and I'm not sure how to parse around it. I need the data from "rate_response", how do I get to it?

The response:

[
    200,
    {
        "rate_response": {
            "rates": [
                {
                    "rate_id": "se-449314776",
                    "rate_type": "shipment",
                    "carrier_id": "se-496556",
                    "shipping_amount": {
                        "currency": "usd",
                        "amount": 0.51
                    },
                    "insurance_amount": {
                        "currency": "usd",
                        "amount": 0.0
                    },
                    "confirmation_amount": {
                        "currency": "usd",
                        "amount": 0.0
                    },
                    "other_amount": {
                        "currency": "usd",
                        "amount": 0.0
                    },
                    "zone": 2,
                    "package_type": "letter",
                    "delivery_days": 2,
                    "guaranteed_service": false,
                 
                },
                {
                    "rate_id": "se-449314777",
                    "rate_type": "shipment",
                    "carrier_id": "se-496556",
                    "shipping_amount": {
                        "currency": "usd",
                        "amount": 1.0
                    },
            
                
            ],
     
    }
]

2 Answers 2

1

If parsedJson is your whole value, then parsedJson[1] is your map that has a single key rate_response, so parsedJson[1]['rate_response'] would be your map with a single key of rates, and parsedJson[1]['rate_response']['rates'] would be a two element list of a couple of maps in a list, so parsedJson[1]['rate_response']['rates'][0] and parsedJson[1]['rate_response']['rates'][1] would access those maps, and so on. It's straightforward to map the layout of most JSON to the Dart tree of Lists and Maps. The really hard part is when the values vary within a level, because then you're stuck with a lot of is to work out which dynamic this is.

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

Comments

1

I assume that you have get the response from your endpoint, new you have to do is get the data you want from your response.

You could try like this example bellow.

final response = await http.get("YOUR URL");
var decodedResponse = jsonDecode(response.body);
var rateList = decodedResponse[0]['rate_response']['rates'];
print(rateList.toString());

2 Comments

I wouldn't call anything that is no longer JSON with a name that has "json" in it. It gets confusing to read.
i updated my code snippet now you can read.... :) @RandalSchwartz

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.