C# Can';t使用Newtonsoft.JSON解析JSON对象

C# Can';t使用Newtonsoft.JSON解析JSON对象,c#,json,json.net,C#,Json,Json.net,我在解析以下JSON对象时遇到问题: {     "SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0" :     {         "origin" : "SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0",         "state" : "connected",         "friendlyNameLong" : "Camera 3",         "friendlyNameShor

我在解析以下JSON对象时遇到问题:

{ 
    "SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0" : 
    {
        "origin" : "SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0",
        "state" : "connected",
        "friendlyNameLong" : "Camera 3",
        "friendlyNameShort" : "3"
    },
    "SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0" : 
    {
        "origin" : "SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0",
        "state" : "disconnected",
        "friendlyNameLong" : "Camera 5", 
        "friendlyNameShort" : "5" 
    },
    ...
据我所知,这台服务器1…,服务器2。。。不应该是变量,应该写“服务器”之类的东西。我正在使用Newtonsoft.JSON解析此数据,但无法对其进行反序列化。我总是得到空值。例如,这是我的视频源类

  private class VideoSource
  {
       public string origin { get; set; }
       public string state { get; set; }
       public string friendlyNameLong { get; set; }
       public string friendlyNameShort { get; set; }

       public override string ToString()
       {
           return origin;
       }
   }
我试图用以下函数解析它:

private VideoSource ParseJsonToVideoSource(string obj)
{
    dynamic source = JsonConvert.DeserializeObject(obj);

    VideoSource s = new VideoSource();
    s.origin = source.origin;
    s.friendlyNameLong = source.friendlyNameLong;
    s.friendlyNameShort = source.friendlyNameShort;
    s.state = source.state;

    return s;
}

正如我所说,我得到的是空值。怎么了?

我会用它形成一个数组,如下所示:

[
    {
        "origin": "SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0",
        "state": "connected",
        "friendlyNameLong": "Camera 3",
        "friendlyNameShort": "3"
    },
    {
        "origin": "SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0",
        "state": "disconnected",
        "friendlyNameLong": "Camera 5",
        "friendlyNameShort": "5"
    }
]
var result = JsonConvert.DeserializeObject<List<VideoSource>>(jsonString);
并将其反序列化如下:

[
    {
        "origin": "SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0",
        "state": "connected",
        "friendlyNameLong": "Camera 3",
        "friendlyNameShort": "3"
    },
    {
        "origin": "SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0",
        "state": "disconnected",
        "friendlyNameLong": "Camera 5",
        "friendlyNameShort": "5"
    }
]
var result = JsonConvert.DeserializeObject<List<VideoSource>>(jsonString);
var result=JsonConvert.DeserializeObject(jsonString);
那它就应该起作用了

更新

当您无法控制json时,请按以下方式操作:

string json =
            "{\"SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0\":{\"origin\":\"SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0\",\"state\":\"connected\",\"friendlyNameLong\":\"Camera 3\",\"friendlyNameShort\":\"3\"},\"SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0\":{\"origin\":\"SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0\",\"state\":\"disconnected\",\"friendlyNameLong\":\"Camera 5\",\"friendlyNameShort\":\"5\"}}";

dynamic result = JsonConvert.DeserializeObject(json);
foreach (JProperty property in result)
{
    var myVideoSource = JsonConvert.DeserializeObject<VideoSource>(property.Value.ToString());
}
stringjson=
“{\'SERVER1/DevicePint.3/SourceEndpoint.video:0\”:{\'origin\:\“SERVER1/DevicePint.3/SourceEndpoint.video:0:0\”,“state\”:“connected\”,“friendlyNameLong\”:“Camera 3\”,“friendlyNameShort\”:“SERVER2/DevicePint.5/SourceEndpoint.video:0:0\”:{“origin\”:“SERVER2/DevicePint.5/SourceEndpoint\”,“state\”,“disconnected\”,“state\”friendlyNameLong\“:“Camera 5\”,“friendlyNameShort\”:“5\”});
动态结果=JsonConvert.DeserializeObject(json);
foreach(结果中的JProperty属性)
{
var myVideoSource=JsonConvert.DeserializeObject(property.Value.ToString());
}

我会用它形成一个数组,如下所示:

[
    {
        "origin": "SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0",
        "state": "connected",
        "friendlyNameLong": "Camera 3",
        "friendlyNameShort": "3"
    },
    {
        "origin": "SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0",
        "state": "disconnected",
        "friendlyNameLong": "Camera 5",
        "friendlyNameShort": "5"
    }
]
var result = JsonConvert.DeserializeObject<List<VideoSource>>(jsonString);
并将其反序列化如下:

[
    {
        "origin": "SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0",
        "state": "connected",
        "friendlyNameLong": "Camera 3",
        "friendlyNameShort": "3"
    },
    {
        "origin": "SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0",
        "state": "disconnected",
        "friendlyNameLong": "Camera 5",
        "friendlyNameShort": "5"
    }
]
var result = JsonConvert.DeserializeObject<List<VideoSource>>(jsonString);
var result=JsonConvert.DeserializeObject(jsonString);
那它就应该起作用了

更新

当您无法控制json时,请按以下方式操作:

string json =
            "{\"SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0\":{\"origin\":\"SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0\",\"state\":\"connected\",\"friendlyNameLong\":\"Camera 3\",\"friendlyNameShort\":\"3\"},\"SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0\":{\"origin\":\"SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0\",\"state\":\"disconnected\",\"friendlyNameLong\":\"Camera 5\",\"friendlyNameShort\":\"5\"}}";

dynamic result = JsonConvert.DeserializeObject(json);
foreach (JProperty property in result)
{
    var myVideoSource = JsonConvert.DeserializeObject<VideoSource>(property.Value.ToString());
}
stringjson=
“{\'SERVER1/DevicePint.3/SourceEndpoint.video:0\”:{\'origin\:\“SERVER1/DevicePint.3/SourceEndpoint.video:0:0\”,“state\”:“connected\”,“friendlyNameLong\”:“Camera 3\”,“friendlyNameShort\”:“SERVER2/DevicePint.5/SourceEndpoint.video:0:0\”:{“origin\”:“SERVER2/DevicePint.5/SourceEndpoint\”,“state\”,“disconnected\”,“state\”friendlyNameLong\“:“Camera 5\”,“friendlyNameShort\”:“5\”});
动态结果=JsonConvert.DeserializeObject(json);
foreach(结果中的JProperty属性)
{
var myVideoSource=JsonConvert.DeserializeObject(property.Value.ToString());
}

一个简单的方法是:

dynamic source = JsonConvert.DeserializeObject<ExpandoObject>(json);
foreach(var videoSource in source)
{
    // Here you can access videoSource.origin, videoSource.state etc.
}

(Mateusz在中已经指出了这一点)

一个简单的方法是:

dynamic source = JsonConvert.DeserializeObject<ExpandoObject>(json);
foreach(var videoSource in source)
{
    // Here you can access videoSource.origin, videoSource.state etc.
}

(Mateusz在中已经注意到了这一点)

工作LinqPad示例:

void Main()
{
    string obj = @"{ 
        ""SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0"" : 
        {
            ""origin"" : ""SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0"",
            ""state"" : ""connected"",
            ""friendlyNameLong"" : ""Camera 3"",
            ""friendlyNameShort"" : ""3""
        },
        ""SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0"" : 
        {
            ""origin"" : ""SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0"",
            ""state"" : ""disconnected"",
            ""friendlyNameLong"" : ""Camera 5"", 
            ""friendlyNameShort"" : ""5"" 
        }
    }";

    VideoSource s = new VideoSource();
    dynamic source = JsonConvert.DeserializeObject<Dictionary<string, VideoSource>>(obj);
    foreach(var videoSource in source)
    {
        s.origin = videoSource.Value.origin;
        s.friendlyNameLong = videoSource.Value.friendlyNameLong;
        s.friendlyNameShort = videoSource.Value.friendlyNameShort;
        s.state = videoSource.Value.state;
        s.Dump();
    }
}

// Define other methods and classes here
public class VideoSource
{
    public string origin { get; set; }
    public string state { get; set; }
    public string friendlyNameLong { get; set; }
    public string friendlyNameShort { get; set; }

    public override string ToString()
    {
        return origin;
    }
}
void Main()
{
字符串obj=@{
“”服务器1/DevicePint.3/SourceEndpoint.video:0:0”“:
{
“来源”:“服务器1/DevicePint.3/SourceEndpoint.video:0:0”,
“”状态“”:“”已连接“”,
“friendlyNameLong”:“摄像机3”,
friendlyNameShort:“3”
},
“SERVER2/DevicePint.5/SourceEndpoint.video:0:0”“:
{
“来源”:“服务器2/DevicePint.5/SourceEndpoint.video:0:0”,
“”状态“”:“”已断开连接“”,
“friendlyNameLong”:“摄像机5”,
friendlyNameShort:“5”
}
}";
VideoSource s=新的VideoSource();
动态源=JsonConvert.DeserializeObject(obj);
foreach(源中的var videoSource)
{
s、 原点=videoSource.Value.origin;
s、 friendlyNameLong=videoSource.Value.friendlyNameLong;
s、 friendlyNameShort=videoSource.Value.friendlyNameShort;
s、 状态=videoSource.Value.state;
s、 Dump();
}
}
//在此处定义其他方法和类
公共类视频源
{
公共字符串源{get;set;}
公共字符串状态{get;set;}
公共字符串friendlyNameLong{get;set;}
公共字符串friendlyNameShort{get;set;}
公共重写字符串ToString()
{
返回原点;
}
}

工作LinqPad示例:

void Main()
{
    string obj = @"{ 
        ""SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0"" : 
        {
            ""origin"" : ""SERVER1/DeviceIpint.3/SourceEndpoint.video:0:0"",
            ""state"" : ""connected"",
            ""friendlyNameLong"" : ""Camera 3"",
            ""friendlyNameShort"" : ""3""
        },
        ""SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0"" : 
        {
            ""origin"" : ""SERVER2/DeviceIpint.5/SourceEndpoint.video:0:0"",
            ""state"" : ""disconnected"",
            ""friendlyNameLong"" : ""Camera 5"", 
            ""friendlyNameShort"" : ""5"" 
        }
    }";

    VideoSource s = new VideoSource();
    dynamic source = JsonConvert.DeserializeObject<Dictionary<string, VideoSource>>(obj);
    foreach(var videoSource in source)
    {
        s.origin = videoSource.Value.origin;
        s.friendlyNameLong = videoSource.Value.friendlyNameLong;
        s.friendlyNameShort = videoSource.Value.friendlyNameShort;
        s.state = videoSource.Value.state;
        s.Dump();
    }
}

// Define other methods and classes here
public class VideoSource
{
    public string origin { get; set; }
    public string state { get; set; }
    public string friendlyNameLong { get; set; }
    public string friendlyNameShort { get; set; }

    public override string ToString()
    {
        return origin;
    }
}
void Main()
{
字符串obj=@{
“”服务器1/DevicePint.3/SourceEndpoint.video:0:0”“:
{
“来源”:“服务器1/DevicePint.3/SourceEndpoint.video:0:0”,
“”状态“”:“”已连接“”,
“friendlyNameLong”:“摄像机3”,
friendlyNameShort:“3”
},
“SERVER2/DevicePint.5/SourceEndpoint.video:0:0”“:
{
“来源”:“服务器2/DevicePint.5/SourceEndpoint.video:0:0”,
“”状态“”:“”已断开连接“”,
“friendlyNameLong”:“摄像机5”,
friendlyNameShort:“5”
}
}";
VideoSource s=新的VideoSource();
动态源=JsonConvert.DeserializeObject(obj);
foreach(源中的var videoSource)
{
s、 原点=videoSource.Value.origin;
s、 friendlyNameLong=videoSource.Value.friendlyNameLong;
s、 friendlyNameShort=videoSource.Value.friendlyNameShort;
s、 状态=videoSource.Value.state;
s、 Dump();
}
}
//在此处定义其他方法和类
公共类视频源
{
公共字符串源{get;set;}
公共字符串状态{get;set;}
公共字符串friendlyNameLong{get;set;}
公共字符串friendlyNameShort{get;set;}
公共重写字符串ToString()
{
返回原点;
}
}

您的json数据实际上是类似于字典键值对的列表。密钥是服务器名。你可以这样解析

        public Dictionary<string, VideoSource> ParseJsonToVideoSource(string obj)
        {
            Dictionary<string, VideoSource> result = new Dictionary<string, VideoSource>();
            dynamic sources = JsonConvert.DeserializeObject(obj);

            foreach (var source in sources)
            {
                VideoSource s = new VideoSource();
                s.origin = source.Value.origin;
                s.friendlyNameLong = source.Value.friendlyNameLong;
                s.friendlyNameShort = source.Value.friendlyNameShort;
                s.state = source.Value.state;

                result.Add(source.Name, s);
            }

            return result;
        }

json数据实际上是类似于字典键值对的列表。密钥是服务器名。你可以这样解析

        public Dictionary<string, VideoSource> ParseJsonToVideoSource(string obj)
        {
            Dictionary<string, VideoSource> result = new Dictionary<string, VideoSource>();
            dynamic sources = JsonConvert.DeserializeObject(obj);

            foreach (var source in sources)
            {
                VideoSource s = new VideoSource();
                s.origin = source.Value.origin;
                s.friendlyNameLong = source.Value.friendlyNameLong;
                s.friendlyNameShort = source.Value.friendlyNameShort;
                s.state = source.Value.state;

                result.Add(source.Name, s);
            }

            return result;
        }

JSON是一个数组,而不是单个对象,您需要将其反序列化为列表或数组。您是否尝试将
dynamic
更改为
var
并查看调试器中的类型?我认为
反序列化对象
返回的是一个对象,而不是动态对象。@RonBeyer是的,它是一个数组。我将如何解析它?当我切换到
var
时,它表示它是
对象的类型。当我尝试像这样反序列化它时:
var source=JsonConvert.DeserializeObject(obj)我得到空值(co