How to parse JSON string to C# Object in Unity

Hello, I have this JSON String and need to get a value.

{
   "data":{
      "attributes":{
         "email":"XXX@XXX.XXX",
         "first_name":"XXX",
         "full_name":"XXX",
         "image_url":"https://c8.patreon.com/2/200/XXX",
         "is_email_verified":true,
         "last_name":"",
         "thumb_url":"https://c8.patreon.com/2/200/XXX",
         "url":"https://www.patreon.com/user?u=XXX",
         "vanity":null
      },
      "id":"XXX",
      "relationships":{
         "memberships":{
            "data":[
               {
                  "id":"XXX",
                  "type":"member"
               }
            ]
         }
      },
      "type":"user"
   },
   "included":[
      {
         "attributes":{
            "currently_entitled_amount_cents":100,
            "last_charge_date":"2021-10-20T10:09:42.000+00:00",
            "last_charge_status":"Paid",
            "lifetime_support_cents":116,
            "patron_status":"active_patron",
            "pledge_relationship_start":"2021-10-20T10:09:40.763+00:00"
         },
         "id":"XXX",
         "type":"member"
      }
   ],
   "links":{
      "self":"https://www.patreon.com/api/oauth2/v2/user/XXX"
   }
}

I need the value of currently_entitled_amount_cents

my try

using System.Collections;
using UnityEngine.Networking;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript2 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // A correct website page.
        StartCoroutine(GetRequest("https://www.example.com"));

    // A non-existing page.
    StartCoroutine(GetRequest("https://error.html"));
}

// Update is called once per frame
void Update()
{

}

IEnumerator GetRequest(string uri)
{
    using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
    {
        // Request and wait for the desired page.
        yield return webRequest.SendWebRequest();

        string[] pages = uri.Split('/');
        int page = pages.Length - 1;

        switch (webRequest.result)
        {
            case UnityWebRequest.Result.ConnectionError:
            case UnityWebRequest.Result.DataProcessingError:
                Debug.LogError(pages[page] + ": Error: " + webRequest.error);
                break;
            case UnityWebRequest.Result.ProtocolError:
                Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
                break;
            case UnityWebRequest.Result.Success:
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
                JsonClass JsonClass = JsonUtility.FromJson<JsonClass>(webRequest.downloadHandler.text);
                Debug.Log(JsonClass.included.attributes.currently_entitled_amount_cents);
                break;
        }
    }
}
}

[System.Serializable]
public class JsonClass
{
    public Included[] included;
}

[System.Serializable]
public class Included
{
    public Attributes attributes;
}

[System.Serializable]
public class Attributes
{
    public int currently_entitled_amount_cents;
}

Use Json.net instead of JsonUtility. JsonUtility is very light weight and I think doesn’t handle an array being at the base class.

I suggest this version GitHub - applejag/Newtonsoft.Json-for-Unity: Newtonsoft.Json (Json.NET) 10.0.3, 11.0.2, 12.0.3, & 13.0.1 for Unity IL2CPP builds, available via Unity Package Manager

Your code would work fine, but you’re missing out on the fact that “included” is an array. So you have to access the first child (or whatever child in case there are multiple):

Debug.Log(JsonClassa.included[0].attributes.currently_entitled_amount_cents);

Next time when you post code that actually throws a compiler error, could you please include the compiler error you get? You haven’t actually mentioned anything about your issue.

Everything the folks above me say, plus a few more tidbits to make working with JSON easier for you:

Problems with Unity “tiny lite” built-in JSON:

In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).

https://forum.unity.com/threads/jso…-not-working-as-expected.722783/#post-4824743

Also, always be sure to leverage sites like:

https://csharp2json.io

PS: for folks howling about how NewtonSoft JSON .NET will “add too much size” to your game, JSON .NET is like 307k in size, and it has the important advantage that it actually works the way you expect a JSON serializer to work in the year 2021.

Yep.

And even if it is true that your game only needs a subset of JSON features and can work with one of the simpler/smaller/faster JSON libraries, you should make it work first, then optimize. That way if you switch away from Json.Net for speed/memory and something breaks, it’ll be easier to track down what, because you have already confirmed that your class structure is right.