简体   繁体   中英

How to extract JSON child data in dart / flutter

I want to extract the child value of queryResult ie. "Hello there. I am chat bot So shall we get started?" from the JSON using flutter

final responseJson = json.decode(response.body);

print("DATA \\n ${responseJson['queryResult']}");

JSON DATA ===>

{  
   "responseId":"123456789",
   "queryResult":{  
      "queryText":"Hello",
      "action":"input.welcome",
      "parameters":{  

      },
      "allRequiredParamsPresent":true,
      "fulfillmentText":"Greetings! How can I assist?",
      "fulfillmentMessages":[  
         {  
            "text":{  
               "text":[  
                  "Hello there. I am chat bot So shall we get started?"
               ]
            }
         },
         {  
            "quickReplies":{  
               "quickReplies":[  
                  "Yes",
                  "No"
               ]
            }
         }
      ],
      "outputContexts":[  
         {  
            "name":"xyz",
            "lifespanCount":5
         }
      ],
      "intent":{  
         "name":"xyz",
         "displayName":"Default Welcome Intent"
      },
      "intentDetectionConfidence":1,
      "diagnosticInfo":{  
         "webhook_latency_ms":5
      },
      "languageCode":"en"
   },
   "webhookStatus":{  
      "message":"Webhook execution successful"
   }
}

I am unable to read the child data of queryResult using

final responseJson = json.decode(response.body);
print("DATA \n ${responseJson['queryResult']}");

If you want to get that specific line, than you can get it this way, but for more usage it would be wise to serialize the json like here: https://flutter.dev/docs/development/data-and-backend/json

print("DATA \n ${responseJson['queryResult']['fulfillmentMessages'][0]['text']['text'][0]}");

For an easier understanding on the path:

print("DATA \n ${
      responseJson['queryResult']
        ['fulfillmentMessages'][0]
          ['text']
            ['text'][0]
      }"
 );

Whole code which is like yours:

import 'dart:convert';

final responseBody = '{"responseId":"123456789","queryResult":{"queryText":"Hello","action":"input.welcome","parameters":{},"allRequiredParamsPresent":true,"fulfillmentText":"Greetings! How can I assist?","fulfillmentMessages":[{"text":{"text":["Hello there. I am chat bot So shall we get started?"]}},{"quickReplies":{"quickReplies":["Yes","No"]}}],"outputContexts":[{"name":"xyz","lifespanCount":5}],"intent":{"name":"xyz","displayName":"Default Welcome Intent"},"intentDetectionConfidence":1,"diagnosticInfo":{"webhook_latency_ms":5},"languageCode":"en"},"webhookStatus":{"message":"Webhook execution successful"}}';

void main() {
  final Map<String, dynamic> responseJson = json.decode(responseBody);

  print("DATA \n ${
      responseJson['queryResult']
        ['fulfillmentMessages'][0]
          ['text']
            ['text'][0]
      }"
 );
}

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