将JSON转换为字符串C#

时间:2015-09-18 14:51:19

标签: c# json asp.net-mvc-4

我在Azure上托管的webservice正在返回一个JSON对象。 JSON响应如下所示:

这是我的JSON响应

{
   "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"
               ]
            ]
         }
      }
   }
}

请告诉我如何在C#中将此响应转换为字符串。我是新来的,请帮助我,你们的帮助将受到高度赞赏。 谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用StreamReader.ReadToEnd()

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

答案 1 :(得分:0)

我猜你真正想问的是如何反序列化JSON。使用Newtonsoft's JSON library的DeserializeObject方法并将其分配给动态对象类型。

dynamic dynamicObject= JsonConvert.DeserializeObject(json);

然后你可以单独引用每个属性。

string type = dynamicObject.Results.output1.type;