3

I have webservice hosted on Azure which is returning a JSON object. The JSON response looks like this:

HERE is my JSON RESPONSE

{
   "Results":{
      "output1":{
         "type":"table",
         "value":{
            "ColumnNames":[
               "Accommadation",
               "Sex",
               "Age",
               "SiblingsAndSpouse",
               "ParentChild",
               "Fare",
               "Embarked",
               "Scored Labels",
               "Scored Probabilities"
            ],
            "ColumnTypes":[
               "Int32",
               "String",
               "Nullable`1",
               "Int32",
               "Int32",
               "Double",
               "String",
               "Int32",
               "Double"
            ],
            "Values":[
               [
                  "2",
                  "male",
                  "35",
                  "0",
                  "0",
                  "20",
                  "C",
                  "0",
                  "0"
               ],
               [
                  "2",
                  "male",
                  "35",
                  "0",
                  "0",
                  "20",
                  "C",
                  "0",
                  "0"
               ]
            ]
         }
      }
   }
}

Kindly tell me that how to convert this response to string in C#. I am new to this please help me out,help from you guys will be highly appreciated. Thanks!!

Farrukh Ahmed
  • 473
  • 4
  • 11
  • 26

2 Answers2

1

I'm guessing what your really asking is how do to deserialize the JSON. Use Newtonsoft's JSON library's DeserializeObject method and assign it to a dynamic object type.

dynamic dynamicObject= JsonConvert.DeserializeObject(json);

then you can reference each property individually.

string type = dynamicObject.Results.output1.type;
josh
  • 530
  • 1
  • 5
  • 19
0

You can use StreamReader.ReadToEnd(),

using (Stream stream = response.GetResponseStream())
{
   StreamReader reader = new StreamReader(stream, Encoding.UTF8);
   String responseString = reader.ReadToEnd();
}
thejustv
  • 2,009
  • 26
  • 42