我需要帮助反序列化和编辑这个JSON:
{
"1":{
"id":1,
"constant":{
},
"common":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":199,
"min":1,
"max":1
},
{
"id":987,
"min":1,
"max":1
},
{
"id":985,
"min":1,
"max":1
}
]
},
"uncommon":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":205,
"min":1,
"max":1
},
{
"id":1249,
"min":1,
"max":1
}
]
},
"rare":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":215,
"min":1,
"max":1
},
{
"id":1201,
"min":1,
"max":1
},
{
"id":1149,
"min":1,
"max":1
}
]
},
"useRareTable":true
},
"2049":{
"id":2049,
"constant":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":592,
"min":1,
"max":1
}
]
},
"common":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":985,
"min":1,
"max":1
}
]
},
"uncommon":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":1197,
"min":1,
"max":1
},
{
"id":1249,
"min":1,
"max":1
}
]
},
"rare":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":1147,
"min":1,
"max":1
},
{
"id":1149,
"min":1,
"max":1
}
]
},
"useRareTable":true
},
"2":{
"id":2,
"constant":{
},
"common":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":5281,
"min":1,
"max":1
},
{
"id":985,
"min":1,
"max":1
}
]
},
"uncommon":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":5301,
"min":1,
"max":1
},
{
"id":1249,
"min":1,
"max":1
}
]
},
"rare":{
"scrolls":[
],
"charms":[
],
"drops":[
{
"id":5303,
"min":1,
"max":1
},
{
"id":1201,
"min":1,
"max":1
},
{
"id":1149,
"min":1,
"max":1
}
]
},
"useRareTable":true
}
}
以下是我的课程:
public class types
{
public List<drops> scrolls { get; set; }
public List<drops> charms { get; set; }
public List<drops> drops { get; set; }
}
public class drops
{
public int id { get; set; }
public int min { get; set; }
public int max { get; set; }
}
public class Definition
{
public int Id { get; set; }
public List<types> constant { get; set; }
public List<types> common { get; set; }
public List<types> uncommon { get; set; }
public List<types> rare { get; set; }
public bool useRareTable { get; set; }
}
public class RootObject
{
public Dictionary<string, Definition> Definitions { get; set; }
}
我正在尝试创建将在列表框中添加每个ID的工具。当我点击列表框中的任何索引时,应该会显示信息。这个id是游戏中的怪物ID,所以每个怪物都有自己的项目下拉列表。之后,我必须能够编辑或添加值并将其序列化为JSON。
答案 0 :(得分:2)
我建议您查看Json.NET。例如,您可以使用以下函数和代码反序列化JSON字符串并访问其中一个对象。
// Deserialize the JSON
Dictionary<string, Definition> root = JsonConvert.DeserializeObject<Dictionary<string, Definition>>(jsonString);
// Find out if an object is available in the dictionary
if (root.ContainsKey("2"))
{
// Get value for key "2"
Definition value = root["2"];
}