使用内部数组反序列化对象(Newtonsoft)

时间:2015-05-10 00:07:17

标签: c# arrays json.net

所以我需要将这个字符串转换为一个对象。

{
  "character_list": [
    {
      "character_id": "5428018587875812257",
      "name": {
        "first": "gixtr2",
        "first_lower": "gixtr2"
      },
      "faction_id": "3",
      "head_id": "1",
      "title_id": "17",
      "times": {
        "creation": "1355333636",
        "creation_date": "2012-12-12 17:33:56.0",
        "last_save": "1385855627",
        "last_save_date": "2013-11-30 23:53:47.0",
        "last_login": "1385850955",
        "last_login_date": "2013-11-30 22:35:55.0",
        "login_count": "334",
        "minutes_played": "16492"
      },
      "certs": {
        "earned_points": "13219",
        "gifted_points": "384",
        "spent_points": "12538",
        "available_points": "1065",
        "percent_to_next": "0.05122222222318"
      },
      "battle_rank": {
        "percent_to_next": "4",
        "value": "50"
      },
      "profile_id": "14",
      "daily_ribbon": {
        "count": "5",
        "time": "1385787600",
        "date": "2013-11-30 05:00:00.0"
      }
    }
  ],
  "returned": 1
}

我得到character_list是一个数组,但INSIDE中还有其他对象。我想帮助将这些对象解析为主对象。

Code

它相当冗长,所以它在文本文件中。我在c#.NET中这样做。

1 个答案:

答案 0 :(得分:1)

你并没有那么远。主要问题是character_list是JSON中的数组,但character_list类中的JsonHttp属性不表示数组或列表,因此它不会正确反序列化。

以下是您需要做的事情:

  1. 首先将您的character_list课程重命名为Character。这个类代表游戏中单个角色的统计数据,不是吗?
  2. 在您的JsonHttp类中,将character_list属性的类型从character_list更改为List<Character>
  3. Character课程中,将daily_ribbons属性重命名为daily_ribbon(单数),或将其标记为[JsonProperty("daily_ribbon")]。 JSON中的属性名称是单数,因此需要以某种方式匹配您的类。
  4. 通过这些更改,您应该能够反序列化您的JSON并提取数据,如下所示(其中json是您在问题中发布的JSON字符串):

    JsonHttp obj = JsonConvert.DeserializeObject<JsonHttp>(json);
    
    foreach (Character c in obj.character_list)
    {
        Console.WriteLine("Character id: " + c.character_id);
        Console.WriteLine("Name: " + c.name.first);
        Console.WriteLine("Faction id: " + c.faction_id);
        Console.WriteLine("Head id: " + c.head_id);
        Console.WriteLine("Title id: " + c.title_id);
        Console.WriteLine("Profile id: " + c.profile_id);
        Console.WriteLine("Creation date: " + c.times.creation_date);
        Console.WriteLine("Last login date: " + c.times.last_login_date);
        Console.WriteLine("Last save date: " + c.times.last_save_date);
        Console.WriteLine("Login count: " + c.times.login_count);
        Console.WriteLine("Minutes played: " + c.times.minutes_played);
        Console.WriteLine("Earned points: " + c.certs.earned_points);
        Console.WriteLine("Gifted points: " + c.certs.gifted_points);
        Console.WriteLine("Spent points: " + c.certs.spent_points);
        Console.WriteLine("Available points: " + c.certs.available_points);
        Console.WriteLine("Percent to next cert: " + c.certs.percent_to_next);
        Console.WriteLine("Battle rank value: " + c.battle_rank.value);
        Console.WriteLine("Percent to next rank: " + c.battle_rank.percent_to_next);
        Console.WriteLine("Daily ribbon count: " + c.daily_ribbons.count);
        Console.WriteLine("Daily ribbon date: " + c.daily_ribbons.date);
    }
    

    这是一个包含完整工作代码的小提琴:https://dotnetfiddle.net/Zt6aWd