{
"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);