简体   繁体   English

使用JSON.NET在C#中解析Json

[英]Parsing Json in C# with JSON.NET

I am developing an application in c# that gets the players backpack value on a game called TF2 using Backpack.tf's api. 我正在用C#开发一个应用程序,该程序使用Backpack.tf的api获取玩家在名为TF2的游戏中的背包价值。

At the moment the code is: 目前的代码是:

    (MAIN CLASS)
    JsonConvert.DeserializeObject<Json1>(json);
    (END OF MAIN CLASS)

public class Json1 {
    public static List<Json2> response { get; set; }
}
public class Json2
{
    public static int success { get; set; }
    public static int current_time { get; set; }
    public static IEnumerable<Json4> players { get; set; }
}
public class Json4 {
    public static int steamid { get; set; }
    public static int success { get; set; }
    public static double backpack_value { get; set; }
    public static string name { get; set; }
}

I've cut out all the other crap out of the main class etc but suffice it to say that yes i've got the json code into the json string ready for deserializing (Tested it with Console.Writeline) 我已经从主类等中删除了所有其他废话,但足以说是的,我已经将json代码放入json字符串中,可以进行反序列化了(已通过Console.Writeline进行了测试)

The problem is. 问题是。 Whenever I use things like Json4.name (when writing to console) it always returns 0. 每当我使用Json4.name之类的东西时(在编写控制台时),它总是返回0。

Sorry if I've made a stupid mistake but I think I've tried things like removing static, changing variable types etc but I still can't get it to work. 抱歉,如果我犯了一个愚蠢的错误,但是我想我已经尝试过删除静态,更改变量类型等操作,但仍然无法正常工作。 Please note this is my first attemopt at deserializing Json code and I wrote the classes at bottom myself because some reason http://json2csharp.com/ didn't work. 请注意,这是我反序列化Json代码的第一个attemopt,由于某些原因http://json2csharp.com/无法正常工作,我自己在底部编写了这些类。 Heres the Json I am trying to deserialize: 这是我要反序列化的Json:

{
   "response":{
      "success":1,
      "current_time":1365261392,
      "players":{
         "0":{
            "steamid":"76561198045802942",
            "success":1,
            "backpack_value":12893.93,
            "backpack_update":1365261284,
            "name":"Brad Pitt",
            "stats_tf_reputation":2257,
            "stats_tf_supporter":1,
            "notifications":0
         },
         "1":{
            "steamid":"76561197960435530",
            "success":1,
            "backpack_value":4544.56,
            "backpack_update":1365254794,
            "name":"Robin",
            "notifications":0
         }
      }
   }
}

(formatting messed up a bit. Also please excuse some spelling mistakes :) ) (格式有点混乱。也请原谅一些拼写错误:))

You have several problems with your code: 您的代码有几个问题:

a) All your fields are static. a)您所有的字段都是静态的。 Remove static; 去除静电; you need them to be instance members. 您需要他们成为实例成员。

b) The response property in Json1 should be just a single instance, not a list. b) Json1的response属性应该只是一个实例,而不是列表。

c) Players needs to be a dictionary (or custom type), not an IEnumerable, since it is not an array in the JSON. c)播放器需要是字典(或自定义类型),而不是IEnumerable,因为它不是JSON中的数组。

d) StreamId has really big numbers that will not fit into an int; d)StreamId确实有很大的数字,无法放入int中; change this to long (or string). 将其更改为long(或字符串)。

public class Json1
{
    public Json2 response { get; set; }
}

public class Json2
{
    public int success { get; set; }
    public int current_time { get; set; }
    public IDictionary<int, Json4> players { get; set; }
}

public class Json4
{
    public long steamid { get; set; }
    public int success { get; set; }
    public double backpack_value { get; set; }
    public string name { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM