0

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.

asked Aug 28, 2019 at 6:37
3
  • Your key can't match with your json. Commented 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#" Commented Aug 28, 2019 at 6:45
  • Look at this Commented Aug 28, 2019 at 6:50

4 Answers 4

3

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;
answered Aug 28, 2019 at 6:52
Sign up to request clarification or add additional context in comments.

Comments

2

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"
answered Aug 28, 2019 at 7:12

Comments

1

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

answered Aug 28, 2019 at 6:46

Comments

1

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

answered Aug 28, 2019 at 8:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.