我有以下JSON,我无法控制它,因为它来自外部API:
{
"user_id": "something_name",
"devices": {
"": {
"sessions": [
{
"connections": [
{
"ip": "225.225.225.225",
"user_agent": "something",
"last_seen": 1504266816737
}
}
]
}
}
}
我正在使用 JSON 反序列化程序 (JSON.Net) 将其转换为动态对象。
dynamic values = JsonConvert.DeserializeObject<dynamic>(mes);
问题是,其中一个 json 键是一个空字符串 "。现在我似乎不能这样称呼该属性:
values.devices.<the empty string part>.sessions.connections
当顶部键之一为空时,如何调用其下方的内容?做devices..sessions
行不通。
可以使用方括号语法访问具有空键的属性。例如:
dynamic ip = values.devices[""].sessions[0].connections[0].ip;
小提琴:https://dotnetfiddle.net/H4orMZ
我发现的第一个问题: 您的样品存在不平衡括号:"connections": [
之后没有关闭]
无论如何,它是可行的:
。使用这样的动态对象:
dynamic values = JsonConvert.DeserializeObject<dynamic>(json);
var ip = values.devices[""].sessions[0].connections[0].ip;
。使用自定义类定义,如下所示:
public class RootObject
{public string user_id { get; set; }
public Dictionary<string, Devices> devices { get; set; }
}
public class Devices
{
public List<Session> sessions { get; set; }
}
public class Session
{
public List<Connections> connections { get; set; }
}
public class Connections
{
public string ip { get; set; }
public string user_agent { get; set; }
public long last_seen { get; set; }
}
因此,设备密钥是一个空字符串 - 但这没关系。
在这里现场尝试。输出:
Deserializing json...
Success!
Device Key #:
Device Value #: Devices
IP #: 225.225.225.225 (just a sample sub-property)
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
完整代码:
public class Program
{
public static void Main()
{
string json = @"{
""user_id"": ""something_name"",
""devices"": {
"""": {
""sessions"": [
{
""connections"": [
{
""ip"": ""225.225.225.225"",
""user_agent"": ""something"",
""last_seen"": 1504266816737
}
]
}
]
}
}
}";
Console.WriteLine("Deserializing json...");
//dynamic values = JsonConvert.DeserializeObject<dynamic>(json);
//var ip = values.devices[""].sessions[0].connections[0].ip;
RootObject o = JsonConvert.DeserializeObject<RootObject>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore //these settings are not important here
});
Console.WriteLine("Success!");
foreach (var dev in o.devices)
{
Console.WriteLine(" Device Key #: {0}", dev.Key);
Console.WriteLine(" Device Value #: {0}", dev.Value);
Console.WriteLine(" IP #: {0}", dev.Value.sessions[0].connections[0].ip);
}
}
}
public class RootObject
{public string user_id { get; set; }
public Dictionary<string, Devices> devices { get; set; }
}
public class Devices
{
public List<Session> sessions { get; set; }
}
public class Session
{
public List<Connections> connections { get; set; }
}
public class Connections
{
public string ip { get; set; }
public string user_agent { get; set; }
public long last_seen { get; set; }
}
另一种解决方案是您可以在代码中设置 json 值的属性名称,如下所示:
{
"errors": {
"": [
" invalid request"
]
},
"title": "One or more validation errors occurred.",
"status": 400
}
模型是这样的:
[JsonPropertyName("")]
public List<string> systemErrors { get ; set; } = new List<string>();
解决此问题的最佳方法是修改 mes 字符串,如下所示:
string modifiedmes = mes.Replace("","prop");
dynamic values = JsonConvert.DeserializeObject<dynamic>(modifiedmes);
我想然后您将能够访问
values.devices.prop.sessions.connections