简体   繁体   中英

Convert JSON to string C#

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!!

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;

You can use StreamReader.ReadToEnd() ,

using (Stream stream = response.GetResponseStream())
{
   StreamReader reader = new StreamReader(stream, Encoding.UTF8);
   String responseString = reader.ReadToEnd();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM