How to get nested json data
Hello,
I am getting data from an API as json and it come out like this:
{
"code":"5000157074454",
"product":{
"unknown_nutrients_tags":[
],
"additives_old_tags":[
],
"informers_tags":[
"openfoodfacts-contributors"
],
"product_name_en":"Tomato Ketchup ",..
"states":"en:to-be-completed, en:nutrition-facts-to-be-completed, en:ingredients-to-be-completed, en:expiration-date-to-be-completed, en:packaging-code-to-be-completed, en:characteristics-to-be-completed, en:categories-to-be-completed, en:brands-completed, en:packaging-to-be-completed, en:quantity-completed, en:product-name-completed, en:photos-to-be-uploaded",
"product_name":"Tomato Ketchup ",
"ingredients_debug":[
],
"product_name_debug_tags":[
],
"entry_dates_tags":[
"2017-09-27",
"2017-09",
"2017"
],
"code":"5000157074454",
"rev":2,
"languages_codes":{
"en":1
},
"nutrition_data_per":"100g",
"additives_debug_tags":[
],
"checkers_tags":[
],
"quantity_debug_tags":[
],
"editors_tags":[
"openfoodfacts-contributors"
],
"allergens":"",
"ingredients_tags":[
],
"interface_version_created":"20120622",
"serving_quantity":0,
"last_modified_by":null,
"languages":{
"en:english":1
},
"lang":"en",
"creator":"openfoodfacts-contributors",
"countries_hierarchy":[
"en:united-kingdom"
],
"countries":"en:GB",
"quantity":"700g",
"nutrient_levels_tags":[
],
"nutriments":{
},
"languages_hierarchy":[
"en:english"
],
"additives_tags":[
],
"ingredients":[
],
"last_editor":null,
"allergens_hierarchy":[
],
"created_t":1506508630,
"photographers_tags":[
],
"ingredients_that_may_be_from_palm_oil_tags":[
],
"brands_debug_tags":[
],
"sortkey":506508686,
"lc":"en",
"allergens_tags":[
],
"additives_prev_tags":[
],
"interface_version_modified":"20150316.jqm2",
"codes_tags":[
"code-13",
"5000157074454",
"500015707445x",
"50001570744xx",
"5000157074xxx",
"500015707xxxx",
"50001570xxxxx",
"5000157xxxxxx",
"500015xxxxxxx",
"50001xxxxxxxx",
"5000xxxxxxxxx",
"500xxxxxxxxxx",
"50xxxxxxxxxxx",
"5xxxxxxxxxxxx"
],
"correctors_tags":[
],
"additives_original_tags":[
],
"no_nutrition_data":"",
"nutrition_grades_tags":[
"unknown"
],
"states_hierarchy":[
"en:to-be-completed",
"en:nutrition-facts-to-be-completed",
"en:ingredients-to-be-completed",
"en:expiration-date-to-be-completed",
"en:packaging-code-to-be-completed",
"en:characteristics-to-be-completed",
"en:categories-to-be-completed",
"en:brands-completed",
"en:packaging-to-be-completed",
"en:quantity-completed",
"en:product-name-completed",
"en:photos-to-be-uploaded"
],
"id":"5000157074454",
"ingredients_text_with_allergens":null,
"ingredients_text_debug":null,
"ingredients_text_with_allergens_en":null,
"brands":"Heinz",
"countries_tags":[
"en:united-kingdom"
],
"last_edit_dates_tags":[
"2017-09-27",
"2017-09",
"2017"
],
"ingredients_ids_debug":[
],
"states_tags":[
"en:to-be-completed",
"en:nutrition-facts-to-be-completed",
"en:ingredients-to-be-completed",
"en:expiration-date-to-be-completed",
"en:packaging-code-to-be-completed",
"en:characteristics-to-be-completed",
"en:categories-to-be-completed",
"en:brands-completed",
"en:packaging-to-be-completed",
"en:quantity-completed",
"en:product-name-completed",
"en:photos-to-be-uploaded"
],
"additives_prev_original_tags":[
],
"_keywords":[
"tomato",
"ketchup",
"heinz"
],
"nutrition_score_debug":"missing energy",
"languages_tags":[
"en:english",
"en:1"
],
"_id":"5000157074454",
"complete":0,
"nutrient_levels":{
},
"misc_tags":[
"en:nutriscore-not-computed",
"en:nutrition-not-enough-data-to-compute-nutrition-score",
"en:nutrition-no-saturated-fat"
],
"ingredients_from_palm_oil_tags":[
],
"last_modified_t":1506508686,
"brands_tags":[
"heinz"
]
},
"status":1,
"status_verbose":"product found"
}
I want to see the items using Debug.Log I can do this for stuff that is on the top level but stuff that is nested, such as the stuff inside the "Product" section, I am unable to get to.
This is the bit of code I use to use the data once it has come through the API:
jsonString = File.ReadAllText(dataPath);
PulledData pulledData = JsonUtility.FromJson<PulledData>(jsonString);
Debug.Log(pulledData.status_verbose);
Any help will be greatly appiciated.
Thanks!
Answer by Bunny83 · Oct 05, 2017 at 01:15 PM
Well, how does your "PulledData" class look like? Though it's actually not very practical to parse this whole JSON structure into a class tree. You may want to use my SimpleJSON framework.
You can simply do things like this:
JSONNode n = JSON.Parse( jsonString );
string code = n["code"].Value;
string productName = n["product"]["product_name"].Value;
// or
JSONNode product = n["product"];
string productName = product["product_name"].Value;
// prints all the "codes_tags" strings
foreach(JSONNode item in product["codes_tags"].Children)
{
Debug.Log(item.Value);
}
// same as above
JSONNode tags = product["codes_tags"];
for(int i = 0; i < tags.Count; i++)
{
Debug.Log(tags[i].Value);
}
All you need is to copy that "framework" into a file called SimpleJSON.cs in your project and when you want to use it, just add
using SimpleJSON;
at the top of your script.
This is extremely useful thank you! In truth, I don't want to really parse all the data there are only 3-4 attributes that I need from it but that the data the API returns for me. However now I have got passed this with your help (I had an extremely un-optimized non-json way of doing it before) I can look into that.
Thank you so much for your help!
Answer by softpulsedev13 · Feb 09, 2019 at 12:56 PM
my json like this {"responseCode":1,"arrlstOurApps":[ {"title":"Fidget Spinner","desc":"The Fidget Spinner is amazing fidget simulator which rotates automatically but on touch it will change direction.\n
- Rotate Fidget Spinner and collect the matching ball raining from top.\n
- As you go, Spinner speed of rotation will continuosly increase.","img_url":"https:\/\/lh3.googleusercontent.com\/Bcc6l7atFMg7NWweljsKe8uipJJcOr9jEvjLt5VTcjTMMZZxATZTujiPJyQfoMi07VU=w300","redirect_url":"https:\/\/play.google.com\/store\/apps\/details?id=com.softpulse.spinnergame"}, {"title":"Balloon Pop and Learn","desc":"Balloon Pop and Learn is fun app for Kids, Toddler, Preschoolers.\nKids will enjoy the app with various balloons animations and sounds, App includs below variantions ;\n
- 1 to 20 Numbers\n
- A to Z small and capital alphabets\n
- Wild life animals\n
- Sea animals\n
- Birds","img_url":"https:\/\/lh3.googleusercontent.com\/Jn6ibTlrMzcNQ9oriXJgJcH21Qyt3lCxxhE5Jnk4big_9UzggU2sn_mV-WUMuzDX2UxD=w300","redirect_url":"https:\/\/play.google.com\/store\/apps\/details?id=com.softpulse.ballonpopandlearn"}]}
how can i deserealize it. and store it in this kind of list class. public class Data { public string data_name; public string data_dec; public string data_ImgUrl ; public string data_url ;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612140304im_/https://answers.unity.com/themes/thub/images/avi.jpg)