- Home /
Get JSON array object string value
Hello, I am very begginer with arrays and I have been looking around forums and nothing worked for me. I am trying to get "speech" and "action" values from this JSON
{
"id": "a0366bae-6918-4d54-a693-36cb50a7c804",
"timestamp": "2017-12-16T12:58:51.046Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Hi",
"action": "HelloBasic",
"actionIncomplete": false,
"parameters": {},
"contexts": [],
"metadata": {
"intentId": "8be4ef2c-8767-41fd-ae01-71c54455e2d2",
"webhookUsed": "false",
"webhookForSlotFillingUsed": "false",
"intentName": "Hello"
},
"fulfillment": {
"speech": "",
"messages": [
{
"type": 0,
"speech": ""
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "5b2211f7-3a0e-45cf-8a87-1e48dbf7a2d6"
}
And I just have no idea how to get the string value from this. Sorry for English
Answer by Bunny83 · Dec 16, 2017 at 04:32 PM
Well you have to use some sort of JSON parser. If you want to use Unity's JSONUtility you have to create classes which resemble the same data structure in C#. However if you only need specific fields you may want to use a generic JSON parser like my SimpleJSON.
With SimpleJSON you can simply do
JSONNode data = JSON.Parse(yourJsonString);
string action = data["result"]["action"].Value;
string speech = data["result"]["fulfillment"]["speech"].Value;
And to get the "speech" field inside the messages array you can do
foreach(JSONNode message in data["result"]["fulfillment"]["messages"])
{
int msgType = message["type"].AsInt;
string msgSpeech = message["speech"].Value;
// do something
}
Answer by Gotal · Jan 10, 2019 at 10:58 PM
I cant even begin to explain how much this helped me dude, i suck at reading .json data, never done it in my life, and with this i managed to do some quality work
gave you all the points I have, you deserve every last one, cheers dude
Your answer
Follow this Question
Related Questions
Converting a string to an int 2 Answers
Most efficient way to convert string[] to int [] in C#? 2 Answers
String Parsing 1 Answer
Access all Items in an Array 4 Answers
Convert to array. 1 Answer