- Home /
Find Something in a JSON
I have a json file, which is an array of objects with a wide variety of values. Here is what one of the objects would look like:
{
"Tag": "AST",
"Name": "Asturias",
"Full Name": "Kingdom of Asturias",
"Religion": "Catholic",
"Culture Group": "Iberian" ,
"Ruler": "Alfonso III",
"Govermnet Type": "Elective Monarchy",
"Ruler Title": "King",
"Sovereignty": "Independent",
"Red": 0,
"Green": 0,
"Blue": 0
}
The last pieces of information, red, green, and blue represent how these objects, which are nations, appear on a map. I already have a code that, when an area is clicked, will use a raycast to determine the color of the clicked area, giving me a color 32 with the rgb color of that area. I'm now struggling with finding a way to use this color value to find the matching nation in the json, and from there determining other properties of the clicked nation, such as the name.
The red green and blue are empty here, because I hadn't added these into this nation apparently, I only used this nation because it was the first on the list.
Two thoughts.
If two countries have the same color, you will have to deter$$anonymous$$e between the two which country it should be which would require extra logic.
Why not just give each country a unique id? You could even check there name if you really wanted to. But if you clicked on austria and its id is 1. You go and find your json object that has an id of 1. No color checking is required.
Answer by MacLeanMX · Jan 17, 2020 at 04:12 PM
You need create a class with the parameters that you have in the JSON and the class need to be Serializable
[Serializable]
public class MyJSON
{
{
public string Tag;
public string Name;
public string Full Name;
public string Religion;
public string Culture Group;
public string Ruler;
public string Govermnet Type;
public string Ruler Title;
public string Sovereignty;
public int Red;
public int Green;
public int Blue;
}
After that you only call the library JsonUtility from Unity Engine and the method FromJson(json);
GetDataFromJson(string jsonwithData)
{
var DataFromJson = JsonUtility.FromJson<MyJSON>(jsonwithData);
Debug.loog(DataFromJson.Red) // 0
Debug.loog(DataFromJson.Blue) // 0
Debug.loog(DataFromJson.Green) // 0
}
if you are working with others JSON only be careful because JsonUtility don't convert arrays and you can have a error
Thank you, now, if I had multiple existent nations, I would create multiple classes right? That much I think I understand, now, when I click somewhere, I get the color I clicked on, so how could I search through the classes to find which class has the color I clicked on?
Well, a concrete example snippet of your actual json would help. Unity's JsonUtility requires an object as root element. An array as root element is not supported. Though an array field inside an object is not an issue.
Alternatively you can use my SimpleJSON parser which doesn't require you to create classes for your different json objects. You can directly read any value
Assu$$anonymous$$g a json text like this:
// omitted non-relevant fields
[
{
"Red": 0,
"Green": 0,
"Blue": 0
},
{
"Red": 0,
"Green": 0,
"Blue": 0
},
]
This would iterate through all of your nation objects and extract the color either as Color32 or as Color.
var nations = JSON.Parse(yourJsonText);
foreach(JSONNode nation in nations)
{
var col = new Color32(nation["Red"].AsInt, nation["Green"].AsInt, nation["Blue"].AsInt, 255);
// Do something with the color. $$anonymous$$eep in $$anonymous$$d this assumes your color is specified as
// bytes (0..255). If your color values are float values (0..1) you want to use:
var col2 = new Color(nation["Red"].AsFloat, nation["Green"].AsFloat, nation["Blue"].AsFloat, 1.0f);
}
Hi, I thing the first step is to convert the json to an object in C#, in the next link there is an example about how to convert a JSON to Array
https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity
After that you can conver the Array to List and use the method find() or use a for to iterate the array and compare the values. I hope this work for you
Your answer
Follow this Question
Related Questions
How to write and read Json in Unity 1 Answer
Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers
JSON deserialization 0 Answers
Turn objects to/from Json for settings. Getting "{}" as output at the moment 1 Answer
Save and Load JSON Data to and from Webserver, Performance Question 0 Answers