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.

Vincent.N
  • 141
  • 1
  • 10

4 Answers4

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;
ALFA
  • 1,726
  • 1
  • 10
  • 19
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"
Naveen Kumar V
  • 2,559
  • 2
  • 29
  • 43
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

Safyan Yaqoob
  • 444
  • 3
  • 11
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

Michael
  • 395
  • 1
  • 13