如何去序列化JSON并在C#中获取值


// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
Console.WriteLine(contentString);
var rs = Newtonsoft.Json.Linq.JToken.Parse(contentString);
Result rst = JsonConvert.DeserializeObject<Result>(contentString);
//Here i need to get the first value in the description as it appears to be a list
var firstValue= rst.description;
//And also a value from caption
var captionValue = rst.Caption

public class Result
{
public Category[] categories { get; set; }
public Description description { get; set; }
public string requestId { get; set; }
public  Caption caption { get; set;}
public Metadata metadata { get; set; }
public Color color { get; set; }
}
public class Description
{
public string[] tags { get; set; }
public Caption[] captions { get; set; }
}
public class Caption
{
public string text { get; set; }
public float confidence { get; set; }
}
public class Metadata
{
public int width { get; set; }
public int height { get; set; }
public string format { get; set; }
}
public class Color
{
public string dominantColorForeground { get; set; }
public string dominantColorBackground { get; set; }
public string[] dominantColors { get; set; }
public string accentColor { get; set; }
public bool isBWImg { get; set; }
}
public class Category
{
public string name { get; set; }
public float score { get; set; }
}
} 
JSON DATA:
{
"categories": [
{
"name": "abstract_",
"score": 0.00390625
},
{
"name": "others_",
"score": 0.0234375
},
{
"name": "outdoor_",
"score": 0.00390625
}
],
"description": {
"tags": [
"road",
"building",
"outdoor",
"street",
"night",
"black",
"city",
"white",
"light",
"sitting",
"riding",
"man",
"side",
"empty",
"rain",
"corner",
"traffic",
"lit",
"hydrant",
"stop",
"board",
"parked",
"bus",
"tall"
],
"captions": [
{
"text": "a close up of an empty city street at night",
"confidence": 0.7965622853462756
}
]
},
"requestId": "dddf1ac9-7e66-4c47-bdef-222f3fe5aa23",
"metadata": {
"width": 3733,
"height": 1986,
"format": "Jpeg"
},
"color": {
"dominantColorForeground": "Black",
"dominantColorBackground": "Black",
"dominantColors": [
"Black",
"Grey"
],
"accentColor": "666666",
"isBWImg": true
}
}

我知道我想要的东西很简单,但对我来说看起来有点复杂。我用过

Result //rst = JsonConvert.DeserializeObject<Result>(contentString);

连接并获取响应,我已经传入了作为JSON数据的contentString。我只想得到我想要的价值。以描述为例会更有帮助。感谢

为了补充Andrew的答案,您可以使用以下类结构:

public class Result
{
public Category[] categories { get; set; }
public Description description { get; set; }
public string requestId { get; set; }
public Metadata metadata { get; set; }
public Color color { get; set; }
}
public class Description
{
public string[] tags { get; set; }
public Caption[] captions { get; set; }
}
public class Caption
{
public string text { get; set; }
public float confidence { get; set; }
}
public class Metadata
{
public int width { get; set; }
public int height { get; set; }
public string format { get; set; }
}
public class Color
{
public string dominantColorForeground { get; set; }
public string dominantColorBackground { get; set; }
public string[] dominantColors { get; set; }
public string accentColor { get; set; }
public bool isBWImg { get; set; }
}
public class Category
{
public string name { get; set; }
public float score { get; set; }
}

然后使用Newsontsoft的Result result = JsonConvert.DeserializeObject<Result>(json);来反序列化Json。

Newtonsoft下载:https://www.nuget.org/packages/Newtonsoft.Json/

这里最好的选择是创建一个表示响应的模型,然后使用Newtonsoft Json.Net的JsonConvert.DeserializeObject将响应反序列化为模型的实例。这将是一种更像OOP的方法,更容易维护和扩展。

请参阅此处的示例。

https://www.newtonsoft.com/json/help/html/SelectToken.htm

详细检查JToken类。您可以使用反序列化使用整个类结构提取的所有内容,也可以直接提取任何特定值

To get the caption under description, first generate a class with the JSON response. You can use the link to do that [json2c#][1]
//Using the result class
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
Result rsult = JsonConvert.DeserializeObject<Result>(contentString);
var dsc = rsult.description;
string cap = dsc.captions[0].text.ToString();
public class Result
{
public Category[] categories { get; set; }
public Description description { get; set; }
public string requestId { get; set; }
public Metadata metadata { get; set; }
public Color color { get; set; }
}
public class Description
{
public string[] tags { get; set; }
public Caption[] captions { get; set; }
}
public class Caption
{
public string text { get; set; }
public float confidence { get; set; }
}
public class Metadata
{
public int width { get; set; }
public int height { get; set; }
public string format { get; set; }
}
public class Color
{
public string dominantColorForeground { get; set; }
public string dominantColorBackground { get; set; }
public string[] dominantColors { get; set; }
public string accentColor { get; set; }
public bool isBWImg { get; set; }
}
public class Category
{
public string name { get; set; }
public float score { get; set; }
[1]: http://json2csharp.com/

最新更新