如何使用空属性名称调用对象属性?

时间:2017-10-28 02:15:42

标签: c# .net json json.net dynamicobject

我有以下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键是一个空字符串&#34;&#34;。现在,我似乎无法将财产称为:

values.devices.<the empty string part>.sessions.connections

当其中一个顶键为空时,如何调用它下方的内容? 执行devices..sessions无效。

3 个答案:

答案 0 :(得分:5)

您可以使用方括号语法使用空键访问该属性。例如:

dynamic ip = values.devices[""].sessions[0].connections[0].ip;

小提琴:https://dotnetfiddle.net/H4orMZ

答案 1 :(得分:2)

我发现的第一个问题:您的样本遭受不平衡的括号:
"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; }
}

因此,Devices Key是一个空字符串 - 但这很好。

直接试用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; }
}

答案 2 :(得分:-3)

解决此问题的最佳方法是修改mes字符串,如:

string modifiedmes = mes.Replace("","prop");

dynamic values = JsonConvert.DeserializeObject<dynamic>(modifiedmes);

我想那时你就可以访问了

values.devices.prop.sessions.connections