C# 使用C解析复杂的JSON#

C# 使用C解析复杂的JSON#,c#,json,parsing,C#,Json,Parsing,我一直在尝试从网站解析这个复杂的JSON文件: 我需要将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}

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

我需要将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项目


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

我强烈建议尽可能使用“newtonsoft.Json”软件包:

首先将nuget软件包安装到项目中:

安装软件包Newtonsoft.Json-版本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();
        }
    }
}
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序2
{
班级计划
{
班级统计
{
公共智力水平;
公共进出口;
}
类InputObj
{
公共字符串源代码;
公共字符串rsn;
公共词典统计;
}
“rsn\”:“热点面包屑屑屑屑屑屑屑屑屑屑屑屑屑屑屑”,统计数字“,”统计数字“:,,”统计数字“,,”统计数字“:,,“总体”整体::“:“水平”以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下以下::““总体”以下以下以下以下以下以下::::““总体”整体:“::::,,以下以下以下以下以下以下以下”整体:::::““水平”级”以下以下以下以下以下以下以下以下以下”水平::::::::::::““水平:等级:以下以下以下以下以下以下以下”级:167,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,等级\“:30、\“经验\”:13448}、\“祈祷\:{”10月10日,10月10日,10月10日,10月10月10日,10月10日,10月10日,10月10月10日,10月10月10日,10月10日,10月10月10日,10月10日,10月10日,10月10月10日,10月10月10日,10月10月10日,10月10日,10月10月10日,10月10月10日,10月10月10月10日,10月10日,10月10日,10月10月10日,10月10日,10月10月10日,10月10日,10月10月10日,10日,10月10月10月10月10日,10月10日,10日,10月10月10日,10月10:::::::::::::::::::::::::::::::::::::::::::::::::::exp\':0},\'smithing\':{\'level\':1,\'exp\':80},\'mining\':{'目前,一些“exp\”:0.5,:5,\“exp\”:5,,\“exp\”:5月5,:5月5,,,\“教学教学”级:5,:5,,\“exp\”:5,\“exp\”:目前:5,:5,,,,,,,,,,,“赫伯伯布尔::”,,,,,,,,,,,,,,,,,,,,,,,,,,“敏捷:,,,,,,,,,,,,,,,,,,“敏捷:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“exp\”:0},\“construction\”:{“level\”:1,\“exp\”:0},\“calling\”:{“level\”:1,\”exp\“:0},\“地下城工程”:1,\“exp\”:0},\“占卜”:1,\“exp\”:0},\“决斗”:1},\“波黑”:1},\“波黑”:1},\“波黑”:1},\“波黑”:1},\“雾”:1};
静态void Main(字符串[]参数)
{
var customer1=JsonConvert.DeserializeObject(sampleString);
//系统控制台写入线(customer1.orig_rsn);
foreach(customer1.stats中的变量x)
{
System.Console.WriteLine(“键:+x.Key”);
System.Console.WriteLine(“级别:“+x.Value.Level”);
System.Console.WriteLine(“Exp:+x.Value.Exp”);
}
System.Console.ReadKey();
}
}
}

我希望这对你有帮助

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

一旦创建了类,您就可以使用下面的命令将其反序列化为所需的任何内容

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

您应该能够在VisualStudio中将其粘贴为类,然后使用newtonsoft将其反序列化到该类中。该结构看起来非常简单,应该可以正常工作。为您生成类的大多数机器人会将这些
level/exp
集视为单独的类,但很容易将其折叠为一个类。反序列化比parsing@Plutonix你是说每个不同的统计数据应该是独立的类,比如:'公共类攻击{public int level{get;set;}public int exp{get;set;}}'不,我说的是相反的。大多数类生成器不会进行优化,会为每个范围、魔法、攻击等创建一个类。但是它们都是一样的,可以折叠任何帮助我如何获得嵌套的值例如:Stats>Attack>exp我现在添加了声明和一个示例来读取统计数据。这正是我想要的,+10000。非常感谢。@AchillesDH别忘了钚合金的评论
Console.WriteLine(rootObject.stats.agility.level)