如何使用空 属性 名称调用对象 属性?
How do I call an object property with empty property name?
我有以下 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
没有用。
解决这个问题最好的方法是像这样修改 mes 字符串:
string modifiedmes = mes.Replace("","prop");
dynamic values = JsonConvert.DeserializeObject<dynamic>(modifiedmes);
我想你将能够访问
values.devices.prop.sessions.connections
您可以使用方括号语法通过空键访问 属性。例如:
dynamic ip = values.devices[""].sessions[0].connections[0].ip;
Fiddle: https://dotnetfiddle.net/H4orMZ
我发现的第一个问题:你的样本有不平衡的括号:
"connections": [
之后没有关闭 ]
总之,是可行的:
...使用这样的动态对象:
dynamic values = JsonConvert.DeserializeObject<dynamic>(json);
var ip = values.devices[""].sessions[0].connections[0].ip;
...自定义 class 定义如下所示:
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; }
}
因此,设备密钥是一个空字符串 - 但没关系。
现场试玩here。
输出:
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 但我无法控制它,因为它来自外部 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
没有用。
解决这个问题最好的方法是像这样修改 mes 字符串:
string modifiedmes = mes.Replace("","prop");
dynamic values = JsonConvert.DeserializeObject<dynamic>(modifiedmes);
我想你将能够访问
values.devices.prop.sessions.connections
您可以使用方括号语法通过空键访问 属性。例如:
dynamic ip = values.devices[""].sessions[0].connections[0].ip;
Fiddle: https://dotnetfiddle.net/H4orMZ
我发现的第一个问题:你的样本有不平衡的括号:
"connections": [
之后没有关闭 ]
总之,是可行的:
...使用这样的动态对象:
dynamic values = JsonConvert.DeserializeObject<dynamic>(json);
var ip = values.devices[""].sessions[0].connections[0].ip;
...自定义 class 定义如下所示:
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; }
}
因此,设备密钥是一个空字符串 - 但没关系。
现场试玩here。 输出:
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; }
}