JSON from Webrequest

How do I get a JSON Object from a URL and parse it’s information from tags?

Use this asset: https://www.assetstore.unity3d.com/en/#!/content/710

public class JSONResponse : MonoBehaviour {

    private string url = "http://www.example.com/data.json"; //{"message":"hello!"}
    private WWW www = null;

    void Start() {
        www = new WWW (url);
        StartCoroutine (ReceiveResponse());
    }

    private IEnumerator ReceiveResponse() {
        yield return www;
       
        JSONObject json = new JSONObject(www.text);
        Debug.Log(json.GetField ("message").str);
    }
}

I didn’t test the code but it should work.

This has been updated to WebRequest, I am still looking for how to get the JSON response from a POST request

Is this enough or you mean you need a working example?

The answer is: it depends – on which JSON library you’re using, and what you need out of the JSON. I’m using the free Newtonsoft JSON.NET as adapted for Unity quirks, available here:

I’m assuming you know how to send a request via UnityWebRequest and check for errors and so on. Let’s say you’re trying to deserialize an object called PlayerData from the request. Both your Unity project and the server should have an identical copy of the PlayerData class definition available. In the coroutine for receiving the response:

    string response = System.Text.Encoding.UTF8.GetString(request.downloadHandler.data);
    PlayerData player = JsonConvert.DeserializeObject<PlayerData>(response);
5 Likes

I suggest the JSON.NET adaption as posted by @MV10 just because it support clear strongly typed models right out of the box, which will make your life much simpler in the long run. Added tip, keep the shared objects in a shared class library, that way you can easily reuse the same objects. If that is not possible, make sure that you align the specifications with whoever makes the web service.

1 Like

Yes definitely do this. I’ve even gone so far as to multi-target my class library project to target Unity’s .NET 3.5 as well as .NET Core which is what my server is written in. It’s one of those situations where a small amount of extra planning and work up front will save you a large amount of effort and risk throughout the rest of the project.

The Json dot.net permacrashed my unity project. Had to trash the folder before Unity would start without instacrashing in Editor. Guess i shouldn’t be surprised as I just noticed this thread is 2015

I know that feeling, @beanie4now . I’ve been searching for a non-deprecated answer to this original question for hours now, with no luck. There is no reason this should be this difficult.

Hello i am just beginner but if is possible please an example,I want to know how to write a JSON File which contain different Texts for example :

Introduction: (Title) (Content) life is good and nice etc. ….(long text)

Description :frowning: Title) (Content) this car is so beautiful etc. …(long text)

and these texts get displayed on Text Mesh Pro component in unity, first text mesh pro for the title and other for the content . and through a buttons Next and Back I can change from text to other. so I want to put the JSON file on a server and get it on unity components … Please can someone Help me I all appreciate that really

Steps to success:

  1. work through any number of the 60000+ various JSON tutorials out there

7145771--855065--Screen Shot 2021-05-16 at 8.10.16 AM.png

  1. do not reply to five-year-old threads with unrelated questions, as it is against forum rules

Thank you , i didnt know that really

1 Like