C#无键反序列化
我使用C#解析一个返回JSON数据的web APIC#无键反序列化,c#,json,json.net,C#,Json,Json.net,我使用C#解析一个返回JSON数据的web API { "response": { "success": 1, "items": { "Items 1's name": { "index": [ 1 ], "data": { "1": {
{
"response": {
"success": 1,
"items": {
"Items 1's name": {
"index": [
1
],
"data": {
"1": {
"special1": {
"special2": [
{
"value": "4 usd"
}
]
}
}
}
}
}
}
}
我已经成功地使用
public class ResponseResult {
[JsonProperty("response")]
public bpResponse Response { get; set; }
}
public class bpResponse {
[JsonProperty("success")]
public string Success { get; set; }
[JsonProperty("items")]
public Item items { get; set; }
}
using (var jr = new JsonTextReader(new StringReader(data))) {
var js = new JsonSerializer();
var u = js.Deserialize<ResponseResult>(jr);
Console.WriteLine(u.Response.Current_Time);
}
公共类响应结果{
[JsonProperty(“响应”)]
公共bpResponse响应{get;set;}
}
公共类BPR响应{
[JsonProperty(“成功”)]
公共字符串成功{get;set;}
[JsonProperty(“项目”)]
公共项项{get;set;}
}
使用(var jr=newjsontextreader(newstringreader(数据))){
var js=new JsonSerializer();
var u=js.反序列化(jr);
Console.WriteLine(u.Response.Current_Time);
}
在这之后,我陷入了困境,因为我意识到,对于物品的名称,没有可用的密钥。有人能指导我如何继续解析数据吗
提前谢谢 假设您拥有有效的JSON,然后可以使用下一个结构对其进行反序列化:
public class ResponseResult
{
[JsonProperty("response")]
public bpResponse Response { get; set; }
}
public class bpResponse
{
[JsonProperty("success")]
public string Success { get; set; }
[JsonProperty("items")]
public Item Items { get; set; }
}
public class Item
{
[JsonProperty("Items 1's name")]
public ItemFirstName ItemFirstName { get; set; }
}
public class ItemFirstName
{
[JsonProperty("index")]
public List<string> Index { get; set; }
[JsonProperty("data")]
public Data Data { get; set; }
}
public class Data
{
[JsonProperty("1")]
public First First { get; set; }
}
public class First
{
[JsonProperty("special1")]
public Special1 Special1 { get; set; }
}
public class Special1
{
[JsonProperty("special2")]
public List<Special2> Special2 { get; set; }
}
public class Special2
{
[JsonProperty("value")]
public string Value { get; set; }
}
公共类响应结果
{
[JsonProperty(“响应”)]
公共bpResponse响应{get;set;}
}
公共类BPR响应
{
[JsonProperty(“成功”)]
公共字符串成功{get;set;}
[JsonProperty(“项目”)]
公共项项{get;set;}
}
公共类项目
{
[JsonProperty(“项目1的名称”)]
public ItemFirstName ItemFirstName{get;set;}
}
公共类ItemFirstName
{
[JsonProperty(“索引”)]
公共列表索引{get;set;}
[JsonProperty(“数据”)]
公共数据数据{get;set;}
}
公共类数据
{
[JsonProperty(“1”)]
公共优先{get;set;}
}
头等舱
{
[JsonProperty(“special1”)]
公共Special1 Special1{get;set;}
}
公共课特辑1
{
[JsonProperty(“special2”)]
公共列表2{get;set;}
}
公共课堂专题2
{
[JsonProperty(“价值”)]
公共字符串值{get;set;}
}
您的一个问题是,它不是有效的JSON,您缺少“4 usd”左右的引号以及结尾处的右括号。您的JSON结构将属性名称作为值包含,如“1”。这个结构没有意义。什么是“special1”和“special2”是动态的?表示文本可以在整个数据中随机更改?@user2301818,然后在数据
类中将第一个
属性类型替换为作业对象