【问题标题】:Json string deserialization C#Json字符串反序列化C#
【发布时间】:2016-06-17 21:53:37
【问题描述】:

I have this json 我想反序列化它,以便我可以获取每个对象的值,例如:

"icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpouLWzKjhzw8zFdC5K092kl5SClMj3PLXFhGpC_Pp8j-3I4IG7i1Hn_UI-Nmj3ItDGe1BoN1mCr1G4xL_vhMS8tcmcn3JhuihwsHvbzQv3309k3tBw8A",

问题是我可以创建我需要的类,因此我可以反序列化 json,因为 json 字符串具有嵌套对象。

【问题讨论】:

  • 您可以使用正确的类反序列化嵌套对象。你试过什么?
  • My Classes我使用JsonInv反序列化
  • 您是否只想要所有icon_url 数据而不关心它们属于哪些对象?
  • 又是什么问题?

标签: c# json jsonobject json-deserialization


【解决方案1】:

我使用json2csharp 来帮助我生成类。经过一些合并和清理,这就是我得到的:

public class InventoryItem
{
    public string id { get; set; }
    public string classid { get; set; }
    public string instanceid { get; set; }
    public string amount { get; set; }
    public int pos { get; set; }
}

public class AppData
{
    public string def_index { get; set; }
    public int? is_itemset_name { get; set; }
    public int? limited { get; set; }
}

public class Description
{
    public string type { get; set; }
    public string value { get; set; }
    public string color { get; set; }
    public AppData app_data { get; set; }
}

public class Action
{
    public string name { get; set; }
    public string link { get; set; }
}

public class Tag
{
    public string internal_name { get; set; }
    public string name { get; set; }
    public string category { get; set; }
    public string category_name { get; set; }
    public string color { get; set; }
}

public class RgDescription
{
    public string appid { get; set; }
    public string classid { get; set; }
    public string instanceid { get; set; }
    public string icon_url { get; set; }
    public string icon_url_large { get; set; }
    public string icon_drag_url { get; set; }
    public string name { get; set; }
    public string market_hash_name { get; set; }
    public string market_name { get; set; }
    public string name_color { get; set; }
    public string background_color { get; set; }
    public string type { get; set; }
    public int tradable { get; set; }
    public int marketable { get; set; }
    public int commodity { get; set; }
    public string market_tradable_restriction { get; set; }
    public List<Description> descriptions { get; set; }
    public List<Action> actions { get; set; }
    public List<Action> market_actions { get; set; }
    public List<Tag> tags { get; set; }
}

public class RootObject
{
    public bool success { get; set; }
    public IDictionary<string, InventoryItem> rgInventory { get; set; }
    public List<object> rgCurrency { get; set; }
    public IDictionary<string, RgDescription> rgDescriptions { get; set; }
    public bool more { get; set; }
    public bool more_start { get; set; }
}

这些似乎工作正常,您可以使用如下代码进行反序列化和序列化:

var obj = JsonConvert.DeserializeObject<RootObject>(oldString);
Console.WriteLine(obj.rgDescriptions["310776560_302028390"].icon_url); // e.g.
var newString = JsonConvert.SerializeObject(obj,
     new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
// null value handling is optional, the above makes it a little more like the source string

【讨论】:

  • 非常感谢你提供了一个线索,所以我可以找到解决方案,因为 ["310776560_302028390"] 如果我提供物品,这可能会改变(["310776560_302028390"] 是 Steam 中的独特物品)我不得不从这个string item = obj.rgInventory["6596605087"].classid + "_" + obj.rgInventory["6596605087"].instanceid;生成“310776560_302028390”,非常感谢你!
  • 哈哈... json2csharp... 是不是要把所有的“public class_invalid_type...”都拿出来了
猜你喜欢
  • 2021-09-02
  • 1970-01-01
  • 2011-10-06
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多