I've tried to get the values from the JSON object that I made a request from Google APIs. My goal is to get the data from transcript.
This is the JSON file.
{
"results": [
{
"alternatives": [
{
"transcript": "how old are you",
"confidence": 0.66882694
}
]
}
]
}
And I've tried to get the output by using this. But it doesn't work.
var result = ["result"][0]["alternative"][0]["transcript"].ToString()
When I query the data, It doesn't show anything, just empty string.
-
Your key can't match with your json.D-Shih– D-Shih2019年08月28日 06:41:12 +00:00Commented Aug 28, 2019 at 6:41
-
Where are you storing your JSON object. You need to work on that object. Have you Googled "parsing a JSON string in C#"Peter Smith– Peter Smith2019年08月28日 06:45:12 +00:00Commented Aug 28, 2019 at 6:45
-
Look at thisPeter Smith– Peter Smith2019年08月28日 06:50:45 +00:00Commented Aug 28, 2019 at 6:50
4 Answers 4
Convert your JSON to a class
Json2CSharp
and you get:
public class Alternative
{
public string transcript { get; set; }
public double confidence { get; set; }
}
public class Result
{
public List<Alternative> alternatives { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
}
Store it somewhere in your code.
Use Newtonsoft.Json package
Install Newtonsoft.Json
NUGet Package in your solution and import it in your code:
using Newtonsoft.Json;
Now you can deserialize your json as you prefer as long as you have it in a string
variable.
var yourObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
You may access your transcript
value using:
var transcript = yourObject.results[0].alternatives[0].transcript;
Comments
SOLUTION (without using external library like NewtonSoft.Json):
- Add reference System.Web.Extensions.
- use an assembly System.Web.Script.Serialization;
CODE:
var jsonString = "{\"results\": [ {\"alternatives\": [ {\"transcript\": \"how old are you\", \"confidence\": 0.66882694 } ] } ]}";
var jsonDeserialized = serializer.Deserialize<dynamic> (jsonString);
Console.WriteLine (jsonDeserialized["results"][0]["alternatives"][0]["transcript"]); // Prints "how old are you"
Comments
Your strings are JSON formatted, so you will need to parse it into a object. For that you can use JSON.NET.
Here is an example on how to parse a JSON string into a dynamic object:
string source = "{\r\n \"id\": \"100000280905615\", \r\n \"name\": \"Jerard Jones\", \r\n \"first_name\": \"Jerard\", \r\n \"last_name\": \"Jones\", \r\n \"link\": \"https://www.facebook.com/Jerard.Jones\", \r\n \"username\": \"Jerard.Jones\", \r\n \"gender\": \"female\", \r\n \"locale\": \"en_US\"\r\n}";
dynamic data = JObject.Parse(source);
Console.WriteLine(data.id);
.
Console which data you want show
Comments
Usually if I knew what the structure of the JSON looks like I would use class to parse it.
But you could always parse the string to JSON object anytime.
using Newtonsoft.Json.Linq;
var json=JObject.Parse(YOUR_JSON_STRING)
var result = json["results"][0]["alternative"][0]["transcript"].ToString()
https://dotnetfiddle.net/KSDcIP
Also your keys that you are requesting doesn't match the keys in the JSON