-1

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
}
}
rrajra
  • 1
  • 2
  • 2
    What is the current output? What do you mean by "all fish"? What data are you assigning to your object which is missing from the output? – David May 11 '20 at 19:04
  • Sorry if I was unclear, I want to have a dynamic amount of fish objects in my game scene, and I want to save all their data (name, position etc etc.) however I can only seem to save data from one. This is where I think the list comes in, but I can't seem to figure out how to implement it correctly – rrajra May 11 '20 at 19:19
  • If you're referring to your `FishID` property, you're setting it to a new empty object with no data. Did you just forget to add values to it? It's really not clear what the problem is. That property contains the only "list" I see in the model structure, and you're just creating an empty list. – David May 11 '20 at 19:23
  • I am trying to add values to the list, but I am not sure how. In r.fishid I tried putting Fishr.Add(gameObject), however it says 'TextWriter' does not contain a definition for 'Add'. I want to put all my instances of fish from my game scene into the list, and then display their information. – rrajra May 11 '20 at 19:35
  • Doesn't sound like a serizliation issue then, you're just not familiar with the syntax to initialize a collection. Try something like: `r.fishid = new FishID { Fishr = new List { new Fish { /* whatever values go here */ } } }` – David May 11 '20 at 19:43
  • Thank you, that worked. – rrajra May 11 '20 at 20:54

1 Answers1

0

Try this library:

using System.Text.Json;
var json = JsonSerializer.Serialize(Fishr);
adriangc
  • 157
  • 1
  • 1
  • 6