0
{
"page": 2,
"per_page": 3,
"total": 12,
"total_pages": 4,
"data": [
    {
        "id": 4,
        "first_name": "Eve",
        "last_name": "Holt",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
    },
    {
        "id": 5,
        "first_name": "Charles",
        "last_name": "Morris",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
    },
    {
        "id": 6,
        "first_name": "Tracey",
        "last_name": "Ramos",
        "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
    }
]

}

The above JSON was format I tried to test.So I added it to a string but it was showing errors.so I edited it to the following code removed double quotes and added single quotes now error was gone.Not sure to use single or double quotes.

string JSONDataString;
JSONDataString = @"{
'page': 2,
'per_page': 3,
'total': 12,
'total_pages': 4,
'data': [
    {
        'id': 4,
        'first_name': 'Eve',
        'last_name': 'last_name',
        'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg'
    },
    {

        'id': 5,
        'first_name': 'Charles',
        'last_name': 'Morris',
        'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg'

    },
    {

        'id': 6,
        'first_name': 'Tracey',
        'last_name': 'Ramos',
        'avatar': 'https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg'

    }
]
}";

I used a @ at the start of the string.I do not know why I used it as I have seen in some examples.I removed all the double quotes and added single quotes.I created another class to handle the data from the JSON which is given below

public class DataHandler {


public int id;
public string firstname;
public string lastname;
public string avatar;

public DataHandler(int ID,string FName,string LName,string Avatar)
{
    this.id = ID;
    this.firstname = FName;
    this.lastname = LName;
    this.avatar = Avatar;

}


}

How to extract data from the first array and second array.First array elements means(page,per_page...total_pages) .Second array means(id,first_name,last_name,avatar from three groups)?

var obj = JsonUtility.FromJson<DataHandler>(JSONDataString);

2 Answers 2

1

The class representing the JSON is wrong. See the correct representation below:

public class Datum
{
    public int id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string avatar { get; set; }
}

public class RootObject
{
    public int page { get; set; }
    public int per_page { get; set; }
    public int total { get; set; }
    public int total_pages { get; set; }
    public List<Datum> data { get; set; }
}

Don't forget to mark your class as serializable. Use the attribute:

[System.Serializable]

Usage

RootObject root=JsonUtility.FromJson<RootObject>(jsonString);
    foreach(var item in root.data){
    Debug.Log(item.id);
    }
Sign up to request clarification or add additional context in comments.

10 Comments

Not sure,I somewhere read you cant use get set with JsonUtility
Well, that is, in my opinion, the valid representation of the JSON. I don't think getting rid of the get set will have any negative effect. That's just the way Unity works.
I'm not too sure. Why don't you just read your data from a text file?. That way, you won't have to worry about manipulating the JSON string.
Error is coming "ArgumentException: JSON parse error: Missing a name for object member. UnityEngine.JsonUtility."
It helps Unity to better understand the structure of the class, for better serialisation or deserialisation. You can read more on it! Mark as accepted if this answer helped you. Have a nice time!
|
0

Your class that represent object is not right. You can generate a class from json in the visual studio "Edit > Paste special > Paste JSON as Classes".

In you case it's:

public class Rootobject
    {
        public int page { get; set; }
        public int per_page { get; set; }
        public int total { get; set; }
        public int total_pages { get; set; }
        public Datum[] data { get; set; }
    }

    public class Datum
    {
        public int id { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string avatar { get; set; }
    }

Your "JSONDataString" json string looks good so just use "Newtonsoft.Json" nuget to parse the object:

var data = JsonConvert.DeserializeObject<Rootobject>(json);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.