Hi,
How parse this Json-data in C#?
parse it into what?
Unity has built in serializing objects into json and back out. But requires an object type to be defined for the serializing to and from:
Where as you can parse to and from a nesting table of key value pairs (dictionary for instance), because really thatâs all json is. And there are various tools out there available to do that.
I want to write, for example:
string photo_75 = obj.photo75;
Yes, I saw JsonUtility, but I donât know how use it because I need to get photo_75 from âitemsâ array.
You need to define an class that mirrors the json by name and type.
I couldnât exactly show you what that class would look like because for some reason you posted the json as an image⌠use code tags please. I canât read one long string of random characters without any proper formatting.
If you want examples unrelated to your specific json⌠well the documentation has it!
But how to get âphoto_75â field from âitemsâ array?
I found very interesting tool in Visual Studio. If select Json data and select in menu â âEditâ â âPaste Specialâ â âPaste JSON as Classesâ it will be generate this code below:
Can I use it with JsonUtility?
public class Rootobject
{
public int count { get; set; }
public Item[] items { get; set; }
}
public class Item
{
public int id { get; set; }
public int album_id { get; set; }
public int owner_id { get; set; }
public string photo_75 { get; set; }
public string photo_130 { get; set; }
public string photo_604 { get; set; }
public int width { get; set; }
public int height { get; set; }
public string text { get; set; }
public int date { get; set; }
public int post_id { get; set; }
}
Ummm⌠honestly havenât used JsonUtility, but I know it uses the unity serialization engine. And unity serialization doesnât respect properties, and only fields.
You may have to remove all the âget/setâ parts, making them raw fields.
I create class PhotoData:
PhotoData.cs
using UnityEngine;
using System.Collections;
public class PhotoData
{
public int count;
public Item[] items;
public static PhotoData CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<PhotoData>(jsonString);
}
}
public class Item
{
public int id;
public int album_id;
public int owner_id;
public string photo_75;
public string photo_130;
public string photo_604;
public int width;
public int height;
public string text;
public int date;
public int post_id;
}
I ran this code:
void Start()
{
//GetPhotos();
string jsonString = "{\"count\":1,\"items\":[{\"id\":396326171,\"album_id\":-6,\"owner_id\":311627231,\"photo_75\":\"http://cs633231.vk.me/v633231231/1096c/1XDEN8hAVNg.jpg\",\"photo_130\":\"http://cs633231.vk.me/v633231231/1096d/HS3JINU5oBo.jpg\",\"photo_604\":\"http://cs633231.vk.me/v633231231/1096e/kEwS-DEk7fg.jpg\",\"width\":500,\"height\":456,\"text\":\"\",\"date\":1453586265,\"post_id\":1}]}";
PhotoData pd = PhotoData.CreateFromJSON(jsonString);
Debug.Log("count = " + pd.count);
}
I see in Console: count = 1
But when I want to get pd.items.Length I see the error:
On this line:
Debug.Log("items.Length = " + pd.items.Length);
I found the mistake
I forgot to write [Serializable]
using System;
[Serializable]
public class PhotoData
{
// ...
}
[Serializable]
public class Item
{
// ...
}
Now I can write:
Debug.Log("pd.items[0].photo_604 = " + pd.items[0].photo_604);
And I see:
How did y
How did you split the json string into an array of objects. Doesnât that method only one for on objects so if you say:
PhotoData pd = PhotoData.CreateFromJSON(jsonString);
you get on object from the json string, but then what if the json string contains several objects, how do you then iterate through it to get an array of these objects.
P.S I was using:
JsonData Objects= JsonMapper.ToObject(jsonstring);
But it stopped working suddenly
Hi @ChisomoMbeza , did you find a solution for splitting the json string into an array of objects? Any help would be greatly appreciated!
Thanks a lot!
Hope till now your problem get solved.
I still dont understand. I am working with php and javascript and parsing json, xml or yaml last 10 maybe 15 years. But I cant understand how to parse in Unity big json like this one:
{
âsubmarinesâ: [
{
âsubmarine_01â: {
âidâ: 1,
âprefabâ: âsubmarine0â,
âpriceâ: 1000,
âini_submarine_speedâ: 15,
âini_torpedo_speedâ: 10,
âini_torpedoesâ: 2,
âini_reloadâ: 10,
âini_damageâ: 10,
âshop_submarine_speedâ: [
{
âidâ: 1,
âpriceâ: 100,
âvalueâ: 5
},
{
âidâ: 2,
âpriceâ: 300,
âvalueâ: 7
},
and so onâŚSubmarine_02 etcâŚ
Any help, I found only examples for very simple json objects which I dont understand why anybody put in json only 5-10 fields?!
Get a free JSON utility from the Unit Asset Store, and youâll be parsing JSON witin a few minutes.
Thank you, first I was try JsonFormatter from BayatGames but it was not documented and hard to understand.
Finnaly I found SimpleJSON yesterday.
Here is a link to JSON parser:
http://wiki.unity3d.com/index.php/SimpleJSON
It was easy to implement in my case:
using SimpleJSON;
first load json file into string variable shop_json then:
var n = JSON.Parse (shop_json);
var submarine_list = n[âsubmarinesâ][0]; // list of submarines
or
int submarine_id = n[âsubmarinesâ][0][âsubmarine_01â][âidâ]; // Id of submarine 1
I was also facing problem in json parsing in Unity. Now i have learnt to parse json by using both Newton Json plugin and json Utility class in Unity.
For both way I used json2sharp site to create class of json. With Newton json Plugin you can use same class but with json Utility you have to made little change. First you have to remove set and get and just add ; with variables. and 2nd is you have to write [Serializable] on top of all class created by json2csharp. For reference you can follow my tutorial on it.
Json Parsing in Unity by using Newton soft Plugin(.Net Json) and JsonUtility Class.
First I have covered Newton json and then Json Utility class.Hope it will help you.