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 ndmrzk · Feb 11, 2020 at 07:00 AM · jsonfor-loopforeach

How to get values from nested JSON data with foreach or for loop?

Hi, I have got the json data, its really long and has a lot of nested data which i need. If the json looks like this :

 result
 [{
    "description":"eyes",
     "measurement_locations":
     [{
         "AOI_radius_o":81.73,
         "AOI_x_o":445.06740399339611,
         "AOI_y_o":1147.187748624817,
         "description":"left eye",
         "measures":
         [    
             {
                 "description":"eyebags",
                 "value":0.095144319232841418,
                 "value_raw":null
             },
             {
                 "description":"dark circles",
                 "value":0.18982207370058865,
                 "value_raw":null
             }
         ],
         "value":0.18982207370058865,
         "value_raw":null,
         "visualization_data":null
     },
     {
         "AOI_radius_o":81.73,
         "AOI_x_o":720.56740399339606,
         "AOI_y_o":1127.6877486248172,
         "description":"right eye"
         ,"measures":
         [
             {
                 "description":"eyebags",
                 "value":0.092195747704466202,
                 "value_raw":null
             },
             {
             "description":"dark circles",
             "value":0.23332688013566055,
             "value_raw":null}],
             "value":0.23332688013566055,
             "value_raw":null,"visualization_data":null
     }],
     "message":"Ok",
     "value":0.21157447691812459,
     "value_raw":null
 }]

i can get the description, and value but I'm not sure how to get the nested data like measurement_locations. How i get the "top level" data is by for loop like :

 public List<string> description;
 public List<float> value;
 
 public void ReceiveResults(string jsonstring)
 {
         Debug.Log("ReceiveResults function");
 
         ApiResponse response = JsonUtility.FromJson<ApiResponse>(jsonstring);
 
         for (int i = 0; i < response.results.Count; i++)
         {
             description.Add(response.results[i].description);
             value.Add(response.results[i].value);
         }
 }
 
 public class ApiResponse
 {
         public List<SkinResult> results;
 }
 
 [System.Serializable]
 public class SkinResult
 {
         public string description;

         //Not sure what to do with this
         public List<Measurement_Location> measurement_locations;

         public float value;
 }
 [System.Serializable]
 public class Measurement_Location
 {
     public float AOI_x_o;
     public float AOI_y_o;
     public string description;
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ndmrzk · Feb 11, 2020 at 08:39 AM

So we ended up making the ApiResponse class Serializable. Then using foreach to get the values we wanted depending on the dot notation

  foreach(var location in response.results[3].measurement_locations)
         {
             foreach (var data in location.visualization_data)
             {
                 List<float> data_x = new List<float>();
                 List<float> data_y = new List<float>();
                 int index = 0;
 
                 foreach (var point_x in data.x)
                 {
                     data_x.Add(point_x);
 
                 }
 
                 foreach (var point_y in data.y)
                 {
                     data_y.Add(point_y);
 
                     Debug.Log("data_x = " + data_x[index] + "and data_y is " + data_y[index]);
                     index++;
                 }
                 
             }
         }
Comment
Add comment · 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

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

122 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

Related Questions

my wave spawner not working properly 1 Answer

Which is a faster find? (foreach) or (for-loop) 2 Answers

For loop update variables value before functions complete 2 Answers

JSON Loading of Position 0 Answers

Get child position from last gameobject in array 2 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