Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用未知对象名反序列化JSON_C#_Json_Json.net - Fatal编程技术网

C# 使用未知对象名反序列化JSON

C# 使用未知对象名反序列化JSON,c#,json,json.net,C#,Json,Json.net,我正在尝试从API反序列化JSON响应。JSON如下所示(MAC地址和位置已更改): 问题在于措施块。由于对象的名称正在更改,我不知道如何将其正确反序列化为C#对象,然而,如果我这样做,我只会得到空目录 这是我的反序列化方法: APIResponse apiResponse = JsonConvert.DeserializeObject<APIResponse>(await content.ReadAsStringAsync()); APIResponse APIResponse=

我正在尝试从API反序列化JSON响应。JSON如下所示(MAC地址和位置已更改):

问题在于措施块。由于对象的名称正在更改,我不知道如何将其正确反序列化为C#对象,然而,如果我这样做,我只会得到空目录

这是我的反序列化方法:

APIResponse apiResponse = JsonConvert.DeserializeObject<APIResponse>(await content.ReadAsStringAsync());
APIResponse APIResponse=JsonConvert.DeserializeObject(wait content.ReadAsStringAsync()); 这是APIResponse类:

public class APIResponse
{
    public Body[] body { get; set; }
    public string status { get; set; }
    public float time_exec { get; set; }
    public int time_server { get; set; }
}

public class Body
{
    public string _id { get; set; }
    public Place place { get; set; }
    public int mark { get; set; }
    public Measures measures { get; set; }
    public string[] modules { get; set; }
}

public class Place
{
    public float[] location { get; set; }
    public float altitude { get; set; }
    public string timezone { get; set; }
}

public class Measures
{
   public Dictionary<string, SingleModule> singlemodules{ get; set; }
}

public class SingleModule
{
    public Res res { get; set; }
    public string[] type { get; set; }
}

public class Res
{
    public MeasuredData measuredData { get; set; }
}
public class MeasuredData
{
    public float[] values { get; set; }
}
公共类响应
{
公共主体[]主体{get;set;}
公共字符串状态{get;set;}
公共浮点时间_exec{get;set;}
公共整数时间_服务器{get;set;}
}
公共阶级团体
{
公共字符串_id{get;set;}
公共场所{get;set;}
公共整数标记{get;set;}
公共措施措施{get;set;}
公共字符串[]模块{get;set;}
}
公共课场所
{
公共浮点[]位置{get;set;}
公共浮动高度{get;set;}
公共字符串时区{get;set;}
}
公营措施
{
公共字典模块{get;set;}
}
公共类单模块
{
公共资源{get;set;}
公共字符串[]类型{get;set;}
}
公共类资源
{
公共度量数据度量数据{get;set;}
}
公共类测量数据
{
公共浮点[]值{get;set;}
}

有什么方法可以轻松地对度量值进行适当的反序列化吗?

看起来您应该能够摆脱
度量值
类。相反,将字典直接放入你的
正文
类:

public class Body
{
    public string _id { get; set; }
    public Place place { get; set; }
    public int mark { get; set; }
    public Dictionary<string, SingleModule> measures { get; set; }
    public string[] modules { get; set; }
}
公共类主体
{
公共字符串_id{get;set;}
公共场所{get;set;}
公共整数标记{get;set;}
公共字典度量{get;set;}
公共字符串[]模块{get;set;}
}

另外,我强烈建议您遵循.NET属性命名约定,并使用
[JsonProperty(“measures”)]
等向Json.NET指示如何将属性转换为Json。

命名混乱,因为它们是从Json生成的,我还没有真正编辑它们。无论如何,这个解决方案是有效的。谢谢我刚刚注意到,
res
的子对象也有类似的问题,它的名称也在变化。我怎样才能解决这个问题?@Niruga:同样的方法-摆脱
Res
类,使用
字典
字典
哎哟,我试着这样做,然后再次询问,但我使用了
字典
,不知道它为什么会抛出异常。改变了它,去掉了
Res
MeasuredData
类,现在它可以工作了!对不起,再次谢谢你!
public class Body
{
    public string _id { get; set; }
    public Place place { get; set; }
    public int mark { get; set; }
    public Dictionary<string, SingleModule> measures { get; set; }
    public string[] modules { get; set; }
}