Reputation: 800
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
},
],
}
]
Upvotes: 1
Views: 38
Reputation: 1269
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());
Upvotes: 1
Reputation: 44046
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.
Upvotes: 1