如何反序列化json集合。 [英] How to deserialize json collection.
问题描述
如何反序列化json集合。
{成功:true,data:{game_id:1,game_name:测试游戏,game_owner_id:1,host_id:1,host_name:测试主机,variant_id:1,variant_name:DeskTop,mode:demoplay,host_game_id:1 ,host_game_display_name:测试主机游戏,host_interface_id:1,host_interface_name:测试接口名称,参数:{host_parameters:[{parameter_id:1,parameter_name:测试基本参数,parameter_value:主机参数值,currency_code:INR}],game_parameters:[{parameter_id:1,parameter_name:测试基准参数,parameter_value: 游戏参数值,currency_code:INR},{parameter_id:2,parameter_name:测试参数,parameter_value:ffffsss,currency_code:INR}]} ,游戏:测试游戏,主持人:测试主机,变体:DeskTop},消息:游戏成功检索}
我尝试过:
动态东西= JsonConvert.DeserializeObject(jsondata);
string str = stuff.success +< br> + stuff.data.game_id +< br> + stuff.data.game_name +< br> + stuff.data.variant_name +< br> + stuff.message +< br> + stuff.data.game_owner_id +< br> + stuff.data.host_id +< br> + stuff.data.host_name +< br> + stuff.data.variant_id +< br> + stuff.data.variant_name +< br> + stuff.data.mode +< br> + stuff.data.host_game_id +< br> + stuff.data.host_game_display_name +< br> + stuff.data.host_interface_id +< br> + stuff.data.host_interface_name;
Response.Write(str);
但是当我尝试从
参数获取数据时我卡住了:{host_parameters:[{parameter_id:1,parameter_name:测试基准参数, parameter_value:主机参数值,currency_code:INR}]
此部分。
如果您不确定,请使用 json2csharp - 从json生成c#类 [ ^ ]从您的JSON示例创建C#类:
public class HostParameter
{
public int parameter_id { get ; set ; }
public string parameter_name { get 跨度>; set ; }
public string parameter_value { get 跨度>; set ; }
public string currency_code { get 跨度>; set ; }
}
public class GameParameter
{
public int parameter_id { get ; set ; }
public string parameter_name { get 跨度>; set ; }
public string parameter_value { get 跨度>; set ; }
public string currency_code { get 跨度>; set ; }
}
public class 参数
{
public 列表< HostParameter> host_parameters { get ; set ; }
public 列表< GameParameter> game_parameters { get ; set ; }
}
public class 数据
{
public int game_id { get ; set ; }
public string game_name { get 跨度>; set ; }
public int game_owner_id { get 跨度>; set ; }
public int host_id { get 跨度>; set ; }
public string host_name { get 跨度>; set ; }
public int variant_id { get 跨度>; set ; }
public string variant_name { get 跨度>; set ; }
public string mode { get 跨度>; set ; }
public int host_game_id { get 跨度>; set ; }
public string host_game_display_name { get 跨度>; set ; }
public int host_interface_id { get 跨度>; set ; }
public string host_interface_name { get 跨度>; set ; }
public 参数参数{ get ; set ; }
public string game { get 跨度>; set ; }
public string host { get 跨度>; set ; }
public string variant { get 跨度>; set ; }
}
public class RootObject
{
public bool success { get ; set ; }
public 数据数据{ get ; set ; }
public string message { get 跨度>; set ; }
}从如何访问每个元素应该很明显。
我知道我迟到了,但是给一个男人一条鱼,你喂他一天;教一个男人钓鱼,你喂他一辈子。
本文将回答这个以及您可能或将要在在C#中使用JSON的许多其他问题&安培; VB [ ^ ]
How to deserialize json collection.
{"success":true,"data":{"game_id":1,"game_name":"Test Game","game_owner_id":1,"host_id":1,"host_name":"Test Host","variant_id":1,"variant_name":"DeskTop","mode":"demoplay","host_game_id":1,"host_game_display_name":"Test Host Game","host_interface_id":1,"host_interface_name":"Test Interface Name","parameters":{"host_parameters":[{"parameter_id":1,"parameter_name":"Test Base Parameter","parameter_value":"Host param value","currency_code":"INR"}],"game_parameters":[{"parameter_id":1,"parameter_name":"Test Base Parameter","parameter_value":"Game param value","currency_code":"INR"},{"parameter_id":2,"parameter_name":"Test Base Parameter","parameter_value":"ffffsss","currency_code":"INR"}]},"game":"Test Game","host":"Test Host","variant":"DeskTop"},"message":"Games retrieved successfully"}
What I have tried:
dynamic stuff = JsonConvert.DeserializeObject(jsondata); string str = stuff.success + "<br>" + stuff.data.game_id + "<br>" + stuff.data.game_name + "<br>" + stuff.data.variant_name + "<br>" + stuff.message + "<br>" + stuff.data.game_owner_id + "<br>" + stuff.data.host_id + "<br>" + stuff.data.host_name + "<br>" + stuff.data.variant_id + "<br>" + stuff.data.variant_name + "<br>" + stuff.data.mode + "<br>" + stuff.data.host_game_id + "<br>" + stuff.data.host_game_display_name + "<br>" + stuff.data.host_interface_id + "<br>" + stuff.data.host_interface_name ; Response.Write(str); but i am stuck when i try to get data from "parameters":{"host_parameters":[{"parameter_id":1,"parameter_name":"Test Base Parameter","parameter_value":"Host param value","currency_code":"INR"}] this section.
If you aren't sure, use something like json2csharp - generate c# classes from json[^] to create C# classes from your JSON sample:
public class HostParameter { public int parameter_id { get; set; } public string parameter_name { get; set; } public string parameter_value { get; set; } public string currency_code { get; set; } } public class GameParameter { public int parameter_id { get; set; } public string parameter_name { get; set; } public string parameter_value { get; set; } public string currency_code { get; set; } } public class Parameters { public List<HostParameter> host_parameters { get; set; } public List<GameParameter> game_parameters { get; set; } } public class Data { public int game_id { get; set; } public string game_name { get; set; } public int game_owner_id { get; set; } public int host_id { get; set; } public string host_name { get; set; } public int variant_id { get; set; } public string variant_name { get; set; } public string mode { get; set; } public int host_game_id { get; set; } public string host_game_display_name { get; set; } public int host_interface_id { get; set; } public string host_interface_name { get; set; } public Parameters parameters { get; set; } public string game { get; set; } public string host { get; set; } public string variant { get; set; } } public class RootObject { public bool success { get; set; } public Data data { get; set; } public string message { get; set; } }It should be pretty obvious from that how to access each element.
I know that I'm late answering but "Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime."
This article will answer this and many other questions that you might or will have on Working with JSON in C# & VB[^]
这篇关于如何反序列化json集合。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!