Jerry Abraham
Jerry Abraham

Reputation: 103

How to Parse JSON Response of the wp login response in unity

I am able to post User credentials to word press API end point and validate the login credentials and it gives me back a JSON as show below , how should I parse this JSON object in the data to get the value of ID from the following JSON Response

             {
                "data": {
                    "ID": "1",
                    "user_login": "[email protected]",
                    "user_pass": "$P$B2yv.gPx6pYXvnO6u59zkGZdUhXddddXRw.",
                    "user_nicename": "jerry-abrahameeee",
                    "user_email": "[email protected]",
                    "user_url": "",
                    "user_registered": "2019-10-09 07:05:56",
                    "user_activation_key": "",
                    "user_status": "0",
                    "display_name": "[email protected]"
                },
                "ID": 1,
                "caps": {
                    "administrator": true
                },
                "cap_key": "wp_capabilities",
                "roles": ["administrator"],
                "allcaps": {
                    "switch_themes": true,
                    "edit_themes": true,
                    "edit_others_user_registrations": true,
                    "publish_user_registrations": true,
                    "read_private_user_registrations": true,
                    "delete_user_registrations": true,
                    "delete_private_user_registrations": true,
                    "delete_published_user_registrations": true,
                    "delete_others_user_registrations": true,
                    "edit_private_user_registrations": true,
                    "edit_published_user_registrations": true,
                    "manage_user_registration_terms": true,
                    "edit_user_registration_terms": true,
                    "delete_user_registration_terms": true,
                    "assign_user_registration_terms": true,
                    "manage_email_logs": true,
                    "administrator": true
                },
                "filter": null
             }

Upvotes: 1

Views: 329

Answers (1)

Alan Bittencourt
Alan Bittencourt

Reputation: 64

You first need to make classes the same as their entities, then you need to use JsonUtility convert a json string to the classes you created. I made an example here using a json file in the resources folder to make it clear how you need to read this data, something like this:

using UnityEngine;
using System;
using System.IO;
using System.Text;
using System.Globalization;

public class ReadJson : MonoBehaviour
{
    public ContainJson classeFromJson;
    // Start is called before the first frame update
    void Start()
    {
        ReadJsonFromResources();
    }

    public void ReadJsonFromResources()
    {
        string json = "";

        using (StreamReader r = new StreamReader("./Assets/Resources/PlaceHolderJSON/data.json", Encoding.GetEncoding(CultureInfo.GetCultureInfo("pt-BR").TextInfo.ANSICodePage)))
        {
            json = r.ReadToEnd();
        }

        ContainJson containJson = JsonUtility.FromJson<ContainJson>(json);

        classeFromJson = containJson;
    }
}

[Serializable]
public class ContainJson
{
    public Data data;
    public int ID;
    public Caps caps;
    public string cap_key;
    public string[] roles;
    public AllCaps allcaps;
    public string filter;

}


[Serializable]
public class Data
{
    public string ID;
    public string user_login;
    public string user_pass;
    public string user_nicename;
    public string user_email;
    public string user_url;
    public string user_registered;
    public string user_activation_key;
    public string user_status;
    public string display_name;

}

[Serializable]
public class Caps
{
    public bool administrator;
}


[Serializable]
public class AllCaps
{
    public bool switch_themes;
    public bool edit_themes;
    public bool edit_others_user_registrations;
    public bool publish_user_registrations;
    public bool read_private_user_registrations;
    public bool delete_user_registrations;
    public bool delete_private_user_registrations;
    public bool delete_published_user_registrations;
    public bool delete_others_user_registrations;
    public bool edit_private_user_registrations;
    public bool edit_published_user_registrations;
    public bool manage_user_registration_terms;
    public bool edit_user_registration_terms;
    public bool delete_user_registration_terms;
    public bool assign_user_registration_terms;
    public bool manage_email_logs;
    public bool administrator;

}

Upvotes: 1

Related Questions