使用C#解析复杂JSON

时间:2017-09-03 16:31:38

标签: c# json parsing

我一直试图从网站上解析这个复杂的JSON文件:

https://www.tip.it/runescape/json/hiscore_user?rsn=hotcrumbs&old_stats=1

我需要将JSON解析为一个对象,然后我可以使用该对象将某些值打印到控制台中。

{"orig_rsn":"hotcrumbs","rsn":"hotcrumbs","stats":{"overall":
{"level":167,"exp":38759},"attack":{"level":20,"exp":4493},"defence":
{"level":20,"exp":4497},"strength":{"level":10,"exp":1185},"constitution":
{"level":26,"exp":8884},"range":{"level":30,"exp":13448},"prayer":
{"level":10,"exp":1188},"magic":{"level":2,"exp":102},"cooking":
{"level":9,"exp":1030},"woodcutting":{"level":15,"exp":2590},"fletching":
{"level":1,"exp":0},"fishing":{"level":5,"exp":500},"firemaking":
{"level":4,"exp":360},"crafting":{"level":1,"exp":0},"smithing":
{"level":1,"exp":80},"mining":{"level":5,"exp":402},"herblore":
{"level":1,"exp":0},"agility":{"level":1,"exp":0},"thieving":
{"level":1,"exp":0},"slayer":{"level":1,"exp":0},"farming":
{"level":1,"exp":0},"runecrafting":{"level":1,"exp":0},"hunter":
{"level":1,"exp":0},"construction":{"level":1,"exp":0},"summoning":
{"level":1,"exp":0},"dungeoneering":{"level":1,"exp":0},"divination":
{"level":1,"exp":0},"duel":{"level":1},"bh":{"level":1},"bhr":
{"level":1},"fog":{"level":1}}}

正如您所看到的,JSON非常复杂,尤其是它是我工作过的第一个JSON项目。

如果有人能指出我正确的方向,我们将不胜感激。

2 个答案:

答案 0 :(得分:0)

我强烈建议使用" newtonsoft.Json"如果可能,请打包:https://www.nuget.org/packages/Newtonsoft.Json/

首先将nuget包安装到您的项目中:

Install-Package Newtonsoft.Json -Version 10.0.3

然后你可以像这个简短的例子一样转换它:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        class Stats
        {
            public int level;
            public int exp;
        }
        class InputObj
        {
            public string orig_rsn;
            public string rsn;
            public Dictionary<string, Stats> stats;
        }
        const string sampleString = "{\"orig_rsn\":\"hotcrumbs\",\"rsn\":\"hotcrumbs\",\"stats\":{\"overall\":{\"level\":167,\"exp\":38759},\"attack\":{\"level\":20,\"exp\":4493},\"defence\":{\"level\":20,\"exp\":4497},\"strength\":{\"level\":10,\"exp\":1185},\"constitution\":{\"level\":26,\"exp\":8884},\"range\":{\"level\":30,\"exp\":13448},\"prayer\":{\"level\":10,\"exp\":1188},\"magic\":{\"level\":2,\"exp\":102},\"cooking\":{\"level\":9,\"exp\":1030},\"woodcutting\":{\"level\":15,\"exp\":2590},\"fletching\":{\"level\":1,\"exp\":0},\"fishing\":{\"level\":5,\"exp\":500},\"firemaking\":{\"level\":4,\"exp\":360},\"crafting\":{\"level\":1,\"exp\":0},\"smithing\":{\"level\":1,\"exp\":80},\"mining\":{\"level\":5,\"exp\":402},\"herblore\":{\"level\":1,\"exp\":0},\"agility\":{\"level\":1,\"exp\":0},\"thieving\":{\"level\":1,\"exp\":0},\"slayer\":{\"level\":1,\"exp\":0},\"farming\":{\"level\":1,\"exp\":0},\"runecrafting\":{\"level\":1,\"exp\":0},\"hunter\":{\"level\":1,\"exp\":0},\"construction\":{\"level\":1,\"exp\":0},\"summoning\":{\"level\":1,\"exp\":0},\"dungeoneering\":{\"level\":1,\"exp\":0},\"divination\":{\"level\":1,\"exp\":0},\"duel\":{\"level\":1},\"bh\":{\"level\":1},\"bhr\":{\"level\":1},\"fog\":{\"level\":1}}}";
        static void Main(string[] args)
        {
            var customer1 = JsonConvert.DeserializeObject<InputObj>(sampleString);
            //System.Console.WriteLine(customer1.orig_rsn);
            foreach( var x in customer1.stats)
            {
                System.Console.WriteLine("Key: "+x.Key);
                System.Console.WriteLine("Level: " + x.Value.level);
                System.Console.WriteLine("Exp: " + x.Value.exp);
            }
            System.Console.ReadKey();
        }
    }
}

我希望这可以帮到你!

答案 1 :(得分:0)

您需要创建类来保存各种json对象。您可以将json粘贴到:http://json2csharp.com/,它将创建您的类。

创建课程后,您可以使用以下内容将反序列化为您需要的任何内容

var rootObject = JsonConvert.DeserializeObject<RootObject>(jsonString);

然后您可以完全访问各种对象/属性,如下例所示:

Console.WriteLine(rootObject.stats.agility.level)