How do i read and write JSON file From server

using UnityEngine;
using System.Collections;
using System.IO;

public class JSONDemo : MonoBehaviour {
    string path;
    string JSONstring;
    void Start()
    {
        path = "http://www.mywebsite.com/Games/JSON/Creature.json";
        WWW www = new WWW(path);
        StartCoroutine (waitandfire (www));

    }

    IEnumerator waitandfire(WWW www)
    {
        yield return www;
        JSONstring = File.ReadAllText (path);
        Creature yumo = JsonUtility.FromJson<Creature> (JSONstring);
        Debug.Log (yumo.Name);
        yumo.Level = 25;
        string newyumo = JsonUtility.ToJson (yumo);
        Debug.Log (newyumo);
    }

}
[System.Serializable]
public class Creature
{
    public string Name;
    public int Level;
    public int[] stats;
}

DirectoryNotFoundException: Could not find a part of the path “C:\Users\Public\Documents\Unity Projects\multiplayer test\http:[www.mywebsite.com\Games\JSON\Creature.json](http://www.mywebsite.com\Games\JSON\Creature.json)”.

I wana read and write JSON filr from my server. Whenver i try this i am getting this error.
Can any expert help here
Thanks

On line 19 you are reading directly from path using File which assumes a local file. You need to use the text property of the WWW object instead to get the text returned by the server.

You also will probably want to check the error property of the WWW object as well, in case there are errors making the web request.

changed line 19 to
JSONstring = File.ReadAllText (www.text);:wink:

getting path contains invalid char error

It should be:
JSONstring = www.text;

File.ReadAllText tries to read all the text in the file you give it. You’re sending in a JSON representation of a creature.

A creature is not a file!

2 Likes

json file store the multiple image?image should be fetch in unity but how can i show a image in canvas??

interesting with this