JsonData jd = new JsonData();
jd["Name"] = "myname";
jd["Damage"] = 15;
//打印出来就是Json格式字符串
List list = JsonMapper.ToObject>(File.ReadAllText("WEAPONAll.txt"));
JsonData jsond = JsonMapper.ToObject(File.ReadAllText("WEAPONAll.txt"));
List list = new List();
for (int i = 0; i < jsond.Count; i++)
{
Weapon weapon = new Weapon();
weapon.Name = jsond[i]["Name"].ToString();
weapon.Damage = int.Parse(jsond[i]["Damage"].ToString());
weapon.Duration = int.Parse(jsond[i]["Duration"].ToString());
list.Add(weapon);
}
JsonData jsond = JsonMapper.ToObject(File.ReadAllText("WEAPONAll.txt"));
List list = new List();
for (int i = 0; i < jsond.Count; i++)
{
Weapon weapon = JsonMapper.ToObject(jsond[i].ToJson()); //注意这里是转换为Json字符串格式
list.Add(weapon);
}
class Weapon
{
private List enemy = new List();
public string Name { set; get; }
public int Damage { set; get; }
public int Duration { set; get; }
public List Enemy { get => enemy; set => enemy = value; }
public Weapon(string name, int damage, int duration, List list)
{
this.Name = name;
this.Damage = damage;
this.Duration = duration;
this.enemy = list;
}
public Weapon() { }
public override string ToString()
{
return string.Format("name:{0}-damage{1}-duration{2},listCount{3}", Name, Damage, Duration,Enemy.Count);
}
}
{
"Name":"jueshi",
"Damage":21,
"Duration":15,
"Enemy":[{"Name":"rifle","Damage":20,"Duration":10},{"Name":"GongJian","Damage":10,"Duration":10},{"Name":"good","Damage":20,"Duration":10}]
}
Weapon wea = JsonMapper.ToObject( File.ReadAllText("WEAPONAll.txt"));
Console.WriteLine(wea.ToString());
Console.ReadKey(); //输出结果可以注意到List的count为3
string data = JsonMapper.ToJson(weapons);
其它几个例子
[
{
"Type": [
{
"ItemID":1,
"ItemName": "地基"
},
{
"ItemID":2,
"ItemName": "墙壁"
},
{
"ItemID":3,
"ItemName": "门"
},
{
"ItemID":4,
"ItemName": "窗户"
}
]
},
{
"Type": [
{
"ItemID":11,
"ItemName": "弓箭"
},
{
"ItemID":12,
"ItemName": "长矛"
}
]
}
]
public List> ByNameGetJsonData(string name)
{
List> temp = new List>();
string jsonStr = Resources.Load("JsonData/" + name).text;
JsonData jsonData = JsonMapper.ToObject(jsonStr);
for (int i = 0; i < jsonData.Count; i++)
{
List tempList = new List();
JsonData jd = jsonData[i]["Type"];
for (int j = 0; j < jd.Count; j++)
{
tempList.Add(JsonMapper.ToObject(jd[j].ToJson()));
}
temp.Add(tempList); //一个数组里面包含对象,每个对象又包含一个数组
}
return temp;
}
[
{
"MapId": 11,
"MapContents":"0,1,1,1,2,0,0,1,2,1,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0",
"MapName":"Wooden Bow"
},
{
"MapId": 12,
"MapContents":"0,0,0,1,2,0,0,0,1,1,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0",
"MapName":"Wooden Spear"
}
]
private Dictionary LoadMapContents(string name)
{
Dictionary temp = new Dictionary();
string jsonStr = Resources.Load("JsonData/" + name).text;
JsonData jsonData = JsonMapper.ToObject(jsonStr);
for (int i = 0; i < jsonData.Count; i++)
{
//取临时数据.
int mapId = int.Parse(jsonData[i]["MapId"].ToString());
string tempStr = jsonData[i]["MapContents"].ToString();
string[] mapContents = tempStr.Split(',');
string mapName = jsonData[i]["MapName"].ToString();
//构造对象.
CraftingMapItem item = new CraftingMapItem(mapId, mapContents, mapName);
temp.Add(mapId, item);
}
return temp;
}
[
{
"ItemName": "Arrow",
"ItemNum": 64
},
{
"ItemName": "Torch",
"ItemNum": 1
},
{
"ItemName": "Axe",
"ItemNum": 1
}
]
public List GetJsonList(string fileName)
{
List tempList = new List();
string tempJsonStr = Resources.Load("JsonData/" + fileName).text;
//解析JSON.
JsonData jsonData = JsonMapper.ToObject(tempJsonStr);
for (int i = 0; i < jsonData.Count; i++)
{
InventoryItem ii = JsonMapper.ToObject(jsonData[i].ToJson());
tempList.Add(ii);
}
return tempList;
}