Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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# .Net解析来自Google的JSON_C#_Json_Parsing - Fatal编程技术网

C# .Net解析来自Google的JSON

C# .Net解析来自Google的JSON,c#,json,parsing,C#,Json,Parsing,所以我使用的是一个GoogleIDAPI; 我以前很幸运有一个API,它具有WDSL或类似的功能,并且很容易解析成一个类。 但是我得到的JSON不容易解析 { "connectDate":"2018-02-12", "application":"com.chrome.windows", "subtype":"wp:https://xxxxxxxxxxxxxxxxxxx", "authorizedEntity":"xxxxxx", "rel":{ "topics":{ "

所以我使用的是一个GoogleIDAPI; 我以前很幸运有一个API,它具有WDSL或类似的功能,并且很容易解析成一个类。 但是我得到的JSON不容易解析

 {
"connectDate":"2018-02-12",
"application":"com.chrome.windows",
"subtype":"wp:https://xxxxxxxxxxxxxxxxxxx",
"authorizedEntity":"xxxxxx",
"rel":{
    "topics":{
        "movies":{
            "addDate":"2018-01-26"
            },
        "anotherTopic":{    
            "addDate":"2018-02-12"
            }
        }
},
"connectionType":"WIFI",
"platform":"WEBPUSH"
} 
电影和其他主题是我创建的主题,因此我无法将它们添加到我的类中。或者我可以吗

当然,有很多方法可以做到这一点,将json作为字符串处理,并使用正则表达式或作为动态对象遍历节点(dynamic dyn=JsonConvert.DeserializeObject(content);),但理想情况下,我认为它应该是一个字典(至少我认为是这样),但我不知道如何实现

由于这是谷歌,我认为有更标准的方法来处理这种JSON

我试着编了一本字典,但没法用。 通过节点,我可以得到数据,但最终得到的代码如下

DateTime.Parse(((Newtonsoft.Json.Linq.JValue)((Newtonsoft.Json.Linq.JProperty)((Newtonsoft.Json.Linq.JContainer)(obj.First)).First).Value).Value.ToString()) 
我试图寻找类似的JSON解析示例,但找不到

我没有分享我在第一次编辑中提取数据的尝试,因为我不认为这是一种方法;这是一个黑客

我为它创建了一个类

       public class SubscriptionDetails
{
    public DateTime connectDate { get; set; }
    public string application { get; set; }
    public string subtype { get; set; }
    public string authorizedEntity { get; set; }
    public string connectionType { get; set; }
    public string platform { get; set; }
    public topics rel { get; set; }
 } 
但是当涉及到定义子类主题时,我被卡住了

所以我试过了

public class topics  : Dictionary<string, object>
公共类主题:字典
哪一个词典条目包含关键主题? 另一个选项需要字典名

public class topics
{
    public Dictionary<string, Dictionary<string, string>> DUMMY { get; set; }
}
公共类主题
{
公共字典伪{get;set;}
}

多亏了Jamiec的建议,我走上了正确的道路。 当然,节点rel已经是一个字典,其中包含一个名为topics的项

所以需要几个类来解析整个过程:

    public class SubscriptionDetails
{
    public DateTime connectDate { get; set; }
    public string application { get; set; }
    public string subtype { get; set; }
    public string authorizedEntity { get; set; }
    public string connectionType { get; set; }
    public string platform { get; set; }
    public Dictionary<string, topicItems>   rel { get; set; }
}

public class topicItems : Dictionary<string, topicData> { }
public class topicData
{
    public DateTime addDate { get; set; }
}
公共类订阅详细信息
{
公共日期时间连接日期{get;set;}
公共字符串应用程序{get;set;}
公共字符串子类型{get;set;}
公共字符串身份{get;set;}
公共字符串连接类型{get;set;}
公共字符串平台{get;set;}
公共字典rel{get;set;}
}
公共类主题项:字典{}
公共类主题数据
{
public DateTime addDate{get;set;}
}
但如果他们在“主题”之外向rel添加另一个节点,则很可能会崩溃。 这似乎不是一种很好的数据结构方式,反序列化类也不是很友好


不管怎么说,它(现在)起作用了。

你在说什么课?忘了说我在C#工作了。我正在努力解析“topics”节点。
topics
将是一个
Dictionary
很抱歉,我不知道如何使用它。如果我向一个类主题添加一个Dictionary,它需要一个名称:public-class-topics{public-Dictionary-DUMMY{get;set;}}我能想到的另一个选项是public-class-topics:Dictionary-will-not-parse(get-exception)。您有一个根级属性
rel
——该属性必须是一个类,其中包含一个属性
主题
,属于我上面发布的字典类型