从Twitter ASP.NET C#解析Json数据 - 除非解析为类型对象,否则为null数据

时间:2016-01-13 22:05:32

标签: c# json asp.net-mvc

所以我已成功解析了众多API中的数据,但我似乎无法让(某些)twitter工作。

我从端点“/1.1/statuses/user_timeline.json”拉

我遇到问题的json数据如下(有关详细信息,请参阅https://dev.twitter.com/overview/api/entities):

"entities":{
    "hashtags": [
        {"text":"myHasTag","indices":[24,53]}
    ],
    "symbols":[],
    "user_mentions":[
        {"screen_name":"twitter","name":"Twitter","id":2353,"id_str":"2353","indices":[5,14]},
        {"screen_name":"TwitterDev","name":"TwitterDev","id":943434,"id_str":"943434","indices":[11,32]}
    ],
    "urls":[]
}

我想提取 hastags user_mentions 但是当我使用 JavaScriptSerializer 解析数据时它们都会变为null

在我的模型中,我有以下内容:

public partial class TwitterData_Entities
{
    List<TwitterData_HashTag> hashtags { get; set; }
    List<TwitterData_UserMentions> user_mentions { get; set; }
}
public partial class TwitterData_HashTag
{
    public string text { get; set; }
    public List<int> indices { get; set; }
}
public partial class TwitterData_UserMentions
{
    public string screen_name { get; set; }
    public string name { get; set; }
    public long id { get; set; }
    public List<int> indices { get; set; }
}

我也尝试将下面的行添加到 TwitterData_Entities 中的每个对象中,但它没有区别

  

[JsonProperty(PropertyName =“user_mentions”)]

唯一可行和填充的是使用:

public partial class TwitterData_Entities
{
    List<object> hashtags { get; set; }
    List<object> user_mentions { get; set; }
}

问题是我不想使用“对象”类型,因为它不能用于我的目的,它也会在我的数据结构中造成不一致。

你们有什么建议来解决这个问题?我在网上找不到任何东西。

1 个答案:

答案 0 :(得分:1)

我设法通过以下方式对其进行反序列化:

symmetricDiff(A, sizeA, B, sizeB, &newSize);

这只是您在{...}中嵌套的JSON,以使其成为有效的JSON和实际的反序列化。

我的课程如下:

var json = "{" +
            "  \"entities\": {" +
            "    \"hashtags\": [" +
            "      {" +
            "        \"text\": \"myHasTag\"," +
            "        \"indices\": [ 24, 53 ]" +
            "      }" +
            "    ]," +
            "    \"symbols\": [ ]," +
            "    \"user_mentions\": [" +
            "      {" +
            "        \"screen_name\": \"twitter\"," +
            "        \"name\": \"Twitter\"," +
            "        \"id\": 2353," +
            "        \"id_str\": \"2353\"," +
            "        \"indices\": [ 5, 14 ]" +
            "      }," +
            "      {" +
            "        \"screen_name\": \"TwitterDev\"," +
            "        \"name\": \"TwitterDev\"," +
            "        \"id\": 943434," +
            "        \"id_str\": \"943434\"," +
            "        \"indices\": [ 11, 32 ]" +
            "      }" +
            "    ]," +
            "    \"urls\": [ ]" +
            "  }" +
            "}";

var javascriptSerializer = new JavaScriptSerializer();
var deserialized = javascriptSerializer.Deserialize<TwitterData>(json);

重要的是对public class TwitterData_Entities { public List<TwitterData_HashTag> hashtags { get; set; } public List<TwitterData_UserMentions> user_mentions { get; set; } } public class TwitterData_HashTag { public string text { get; set; } public List<int> indices { get; set; } } public class TwitterData_UserMentions { public string screen_name { get; set; } public string name { get; set; } public long id { get; set; } public List<int> indices { get; set; } } public class TwitterData { public TwitterData_Entities entities { get; set; } } 类的属性使用public访问修饰符。