I am trying to serialize a nested list for multiple objects, but I cant seem to get it to work. It will write the fishinformation once, however I want it do it for all fish.
var r = new Alldata();
r.Playerinformation = new PlayerInformation
{
Name = ps.name,
Moneystat = ps.maxMoney,
};
r.fishid = new FishID
{
};
r.fishinformation = new Fish
{
PosInWorld = fish.transform.position,
sellprice = fish.sellprice,
SSpeed = fish.swimspeed,
Nutrition = fish.nutrition
};
var dataString = JsonConvert.SerializeObject(r, Formatting.Indented);
File.WriteAllText(savepath, dataString);
Debug.Log(dataString);
Here is my class which contains everthing
public class Alldata
{
public PlayerInformation Playerinformation { get; set; }
public FishID fishid { get; set; }
public Fish fishinformation { get; set; }
public string name;
}
public class FishID
{
[JsonProperty("fish")]
public List<Fish> Fishr { get; set; }
}
And my goal JSON output should look something like this
{
"name": null,
"Playerinformation": {
"name": "Player",
"Moneystat": 22222,
"FishesID": null
},
"fishid": {
"fish": [{
"UID": 1000,
"assignedname": "fish01",
}, {
"UID": 1001,
"assignedname": "fish02",
}, {
"UID": 1002,
"assignedname": "fish03",
}]
}
}
However, my current output looks like this:
"name": null,
"Playerinformation": {
"name": "Player",
"Moneystat": 527,
"Fishid": null
},
"fishid": {
"fish": null
},
"fishinformation": {
"UID": 0,
"assignedname": null,
"PosInWorld": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"Nutrition": 75.0,
"sellprice": 1.0,
"TimesStarved": 0,
"AteBadCount": 0,
"AteMediumCount": 0,
"AteGreatCount": 0,
"TypeOfFish": 0,
"fishiestosave": null,
"SSpeed": 2.5
}
}