2

I've currently got a json file that looks some what like this called "Monsters.json"

{
"frosline":{
"id":1,
"name":"frosline",
"baseStats":[4, 3, ...],
...}
}

I've also got a MonsterStats class with all the same variables and im having trouble adding more monsters to this json file through code (for example one file may contain the list of monsters that have been caught ala pokemon)

I've tried doing something like:

MonsterStats stats = new MonsterStats(<stats for the monster here>)
JsonData monsterJson = JsonMapper.ToJson(stats);
String oldsave = File.ReadAllText(Application.dataPath + "/Monster.json");

JsonData monsterData = JsonMapper.ToObject(oldsave);
monsterData["newMon"] = monsterJson;

But instead of adding a new object in "newMon", it instead is added as a weird string:

newMon":"{\"id\":0,\"name\":\"player\",\"baseStats\":[10,0,0,0,0,0],\"xpyield\":0,\"evyield\":[0,0,0,0,0,0],\"moves\":[\"Ember\",\"\",\"\",\"\"],\"health\":11,\"stats\":[11,5,5,5,5,5],\"evs\":[0,0,0,0,0,0],\"level\":1,\"xp\":0}"}

I've tried looking for a similar question or a solution but I'm quite new to JSon so it's been difficult to know what to search. This is using litjson if that helps.

What's also annoying me is if I simply print out variable monsterJson it comes out cleanly:

{"id":0,"name":"player","baseStats":[10,0,0,0,0,0],"xpyield":0,"evyield":[0,0,0,0,0,0],"moves":["Ember","","",""],"health":11,"stats":[11,5,5,5,5,5],"evs":[0,0,0,0,0,0],"level":1,"xp":0}
Beansaurus
  • 37
  • 3

2 Answers2

0

So ah, I kind of got it working and it's definitely not the best answer so would still love a reply from someone who knows they're doing.

Changed the last line of the code:

    monsterData["newMon"] = JsonMapper.ToObject(monsterJson.ToString());
Beansaurus
  • 37
  • 3
0

Stop using external libraries for this. I answered this question few days ago and will just link to the answer. The first solution 1. ONE DATA(NON ARRAY JSON) I provided should work for you because the data you are receiving is not array. Use JsonUtility.ToJson and JsonUtility.FromJson, a unity built in Json parser.

https://stackoverflow.com/a/36244111/3785314

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks for the reply I'll check it out. But like I said I want to potentially have a long list of different monsters, Would that not classify as array? – Beansaurus Mar 31 '16 at 05:10
  • What I mean by array is when id,name, player or other variables appear more than once. In your `{"id":0,"name":"player","baseStats":[10,0,0,0,0,0],"xpyield":0,"evyield":[0,0,0,0,0,0],"moves":["Ember","","",""],"health":11,"stats":[11,5,5,5,5,5],"evs":[0,0,0,0,0,0],"level":1,"xp":0}` everything appeared only once so you are fine with the first answer. If you receive a data where things such as name, id or player appear more than once, then the second answer I provided on that link should work for you because then it becomes an array. – Programmer Mar 31 '16 at 05:23
  • Yeah, been trying for awhile with no progress. As i'm gonna have multiple monsters (and so id, etc will appear more than once) I set it up using the array method. Any chance you could take a look at the question I posted here? http://stackoverflow.com/questions/36325855/having-issues-with-new-jsonutitilty-jsonhelper-in-unity-returning-null-when-it – Beansaurus Mar 31 '16 at 08:48