Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alterix97 · Jan 17, 2020 at 03:06 PM · json

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deathdefy · Jan 17, 2020 at 03:42 PM 0
Share

Two thoughts.

  1. 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.

  2. 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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alterix97 · Jan 17, 2020 at 04:29 PM 0
Share

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?

avatar image Bunny83 alterix97 · Jan 17, 2020 at 06:20 PM 1
Share

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);
 }

avatar image MacLeanMX alterix97 · Jan 17, 2020 at 07:47 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges