Read JSON file data from server to unity c#.

How to parse JSON file which is saved in server and assign the data of JSON file to the variables declared in Unity file.

Use WWW to read the file from the server and save the json in a string.

Then use SimpleJSON to parse it and fill your variables:
http://wiki.unity3d.com/index.php/SimpleJSON

Examples are provided with the Package explaining how SimpleJSON works.

Used LitJson for decoding the JSON and decoded the JSON file from server.

The sample JSON file which is parsed

{
  "title" : "Decode JSON",
  "ID" : 20,
  "buttons" :
  [
    {
      "title" : "Red ",
      "image" : "Image Url"
    },
    {
      "title" : "Green ",
      "image" : "Image Url"
    },
    {
      "title" : "Blue ",
      "image" : "Image Url"
    },
    {
      "title" : "Yellow ",
      "image" : "Image Url"
    }
  ]
}

Download and add LitJson.dll to the project.

C# script to decode the JSON file

using UnityEngine;
using LitJson;
using System;
using System.Collections;

public class parseJSON
{
	public string title;
	public string id;
	public ArrayList but_title;
	public ArrayList but_image;
}
public class JSON_D : MonoBehaviour
{
	// Sample JSON for the following script has attached.
	IEnumerator Start()
	{
		string url = " URL of the JSON to be Decode";
		WWW www = new WWW(url);
		yield return www;
		if (www.error == null)
		{
			Processjson(www.data);
		}
		else
		{
			Debug.Log("ERROR: " + www.error);
		}		
	}	
	private void Processjson(string jsonString)
	{
		JsonData jsonvale = JsonMapper.ToObject(jsonString);
		parseJSON parsejson;
		parsejson = new parseJSON();
		parsejson.title = jsonvale["title"].ToString();
		parsejson.id = jsonvale["ID"].ToString();
		
		parsejson.but_title = new ArrayList ();
		parsejson.but_image = new ArrayList ();
		
		for(int i = 0; i<jsonvale["buttons"].Count; i++)
		{
			parsejson.but_title.Add(jsonvale["buttons"]*["title"].ToString());*

parsejson.but_image.Add(jsonvale[“buttons”][“image”].ToString());
* } *
* }*
}

I put together a tutorial doing just that and a bit more.


I use UnityWebRequest to get the JSON and the Newtonsoft.Json to deserialize it. All using async instead of coroutines.

Hope it helps :slight_smile:

I was also facing problem in json parsing in Unity. Now i have learnt to parse json by using both Newton Json plugin and json Utility class in Unity.
For both way I used json2sharp site to create class of json. With Newton json Plugin you can use same class but with json Utility you have to made little change. First you have to remove set and get and just add ; with variables. and 2nd is you have to write [Serializable] on top of all class created by json2csharp. For reference you can follow my tutorial on it.
Json Parsing in Unity by using Newton soft Plugin(.Net Json) and JsonUtility Class.
First I have covered Newton json and then Json Utility class.Hope it will help you.

@sas_88
https://docs.unity3d.com/Manual/JSONSerialization.html
Unity Already have builtin JSON support , enjoy it