0

I get a json response after making a query to an api.

The JSON is like:

 {
 "results": [
 {
 "alternatives": [
 {
 "confidence": 0.965,
 "transcript": "how do I raise the self esteem of a child in his academic achievement at the same time "
 }
 ],
 "final": true
 },
 {
 "alternatives": [
 {
 "confidence": 0.919,
 "transcript": "it's not me out of ten years of pseudo teaching and helped me realize "
 }
 ],
 "final": true
 },
 {
 "alternatives": [
 {
 "confidence": 0.687,
 "transcript": "is so powerful that it can turn bad morals the good you can turn awful practice and the powerful once they can teams men and transform them into angel "
 }
 ],
 "final": true
 },
 {
 "alternatives": [
 {
 "confidence": 0.278,
 "transcript": "you know if not on purpose Arteaga Williams who got in my mother "
 }
 ],
 "final": true
 },
 {
 "alternatives": [
 {
 "confidence": 0.621,
 "transcript": "for what pink you very much "
 }
 ],
 "final": true
 }
 ],
 "result_index": 0
}

I have to do two things to above json result (I keep it as a string*):

  1. Get the transcript part(s) of the json response.
  2. Process those strings.

    • I am new to this. Converting to string is only called serialization. Why would deserialization help here ?

Converting to string: I did it using:

 var reader = new StreamReader(response.GetResponseStream());
 responseFromServer = reader.ReadToEnd();

How to achieve this ?

Jan Köhler
6,1205 gold badges28 silver badges38 bronze badges
asked Jun 25, 2016 at 12:28
2
  • There is no need for deserialization - but it would make your life easier :o) Commented Jun 25, 2016 at 12:29
  • Deserializing the JSON converts it back into a .NET object. You could then access the properties of that object rather than doing a bunch of string parsing. Use a library like Newtonsoft JSON.NET to help with the deserialization. Commented Jun 25, 2016 at 12:36

2 Answers 2

5

You could parse the JSON into concrete classes and work with those hereafter.

To do so, you could use a service like json2csharp which generates classes based on the JSON you provided. Alternatively, you could use the Visual Studio built-in feature Paste JSON As Classes:

enter image description here

public class Alternative
{
 public double confidence { get; set; }
 public string transcript { get; set; }
}
public class Result
{
 public List<Alternative> alternatives { get; set; }
 public bool final { get; set; }
}
public class RootObject
{
 public List<Result> results { get; set; }
 public int result_index { get; set; }
}

You can then use JSON.NET to parse the stringified JSON to concrete class instances:

var root = JsonConvert.DeserializeObject<RootObject>(responseFromServer);
answered Jun 25, 2016 at 12:40
Sign up to request clarification or add additional context in comments.

1 Comment

Impressed by the "Paste JSON as Classes" feature!
1

You should deserialize this. It's the easiest way to deal with it. Using Json.NET and dynamic that might look like:

dynamic jsonObj = JsonConvert.DeserializeObject(responseFromServer);
foreach (var result in jsonObj.results) {
 foreach (var alternative in result.alternatives) {
 Console.WriteLine(alternative.transcript);
 }
}

But you might want to make explicit classes for it instead. Then you can do:

MyRootObject root = JsonConvert.DeserializeObject<MyRootObject>(responseFromServer);

And deal with it like any other .NET object.

answered Jun 25, 2016 at 12:37

1 Comment

With JSON in clipboard VS offers menu Edit/Paste Special/Paste JSON as classes

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.