JSONFX and Unity

Hey all. It’s been fucking ages since my last post, but maybe this time I’ll be able to keep this sucker going. But to hell with excuses and whatnot. On to the point!

Recently I have been working with unity to build some games at work and decided to take a crack at a 2d game for myself. One of the first things I’ve decided to do is work on the quest engine for the game. Since this is going to be a single player game I’m storing my quests in a local file. I initially tried to store this data in XML until I discovered how awful XML is in C#. I then moved over to storing my quests in JSON. After some googling I came across JsonFX and went with it since it will also work on mobile.

Looking at the examples it seem pretty straight forward so off I went. And after a little trial and error I got it at least loading and parsing the json into my unity project finally!

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using JsonFx.Json;

public class FetchQuest{
	public string name;
	public int questId;
}

public class StoryQuest{
	public string name;
	public int npcId;
}

public class Quest2{
	public FetchQuest[] fetchquests;
	public StoryQuest[] storyquests;
}

public class QuestManager : MonoBehaviour {
	void Start () {
		StreamReader r = File.OpenText(Application.dataPath+"/quest.json");
	    string _info = r.ReadToEnd();
	    r.Close();
		print(_info);
		var search = JsonReader.Deserialize<Quest2>(_info);
		print(search.fetchquests[0].name);
	}
}


[text]
{
"fetchquests":
[{"name":"test", "questId":1},
{"name":"test", "questId":1}]
,"storyquests":
[{"name":"story 1", "npcId":1},
{"name":"story 2", "npcId":2}]
}
[/text]

And thats loading and deserializing! This makes me happy. A thing to note and this is what caught me, is that it looks up the class variables by name, so case matters! Remember this!

14 Comments

  1. I can get it to work when reading a simple JSON string but when there are "subnodes" I can't get it to work. It complains that I "Cannot cast from source type to destination type".

  2. I’m looking to integrate JSON into my game engine (rather than serializing xml). Has anybody else had any experience and can recommend a good json library? Does Unity ship with a mono-based json implementation?

  3. I’m looking to integrate JSON into my game engine (rather than serializing xml). Has anybody else had any experience and can recommend a good json library? Does Unity ship with a mono-based json implementation?

Leave a Reply

Your email address will not be published. Required fields are marked *