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 bastolen · Mar 28, 2019 at 03:58 PM · arrayjsongoogleparsing

JSON to object for multidimensional array

For a webgl game we are making for a school project we have to implement localization. We decided to do this with google sheets. But we cannot get the result parsed correctly.

We are doing a get request to: https://sheets.googleapis.com/v4/spreadsheets/[FILE_ID]/values/A1:D4?key=[API_KEY]⪯ttyPrint=false.

The result we are getting back is:

 {
     "range": "Sheet1!A1:D4",
     "majorDimension": "ROWS",
     "values": [
         ["key", "en-gb", "en-us", "nl-nl"],
         ["MAIN_MENU", "main menu", "main menu", "hoofdmenu"],
         ["SHOP", "store", "shop", "winkel"],
         ["CHARACTER_SELECTION", "character selection", "character selection", "Karakter keuze"]
     ]
 }

When we try to parse it with:

 [Serializable]
 public struct API_RESULT {
     public string range;
     public string majorDimension;
     public string[][] values;
 }
 
 API_RESULT data = JsonUtility.FromJson<API_RESULT>(req.downloadHandler.text);

The range and the majorDimension are working correctly, but for the values we get an array with the length 4 but the items are empty.

Is there something that we are forgetting to do, or is it just not possible to do this?

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 Bunny83 · Mar 28, 2019 at 08:49 PM

Unity's JsonUtility has the same restrictions as the normal serialization system in Unity as it's meant as an extension. So it doesn't support nested / jagged arrays like that. I highly recommend to use a different json parser. I've written SimpleJSON as a single file parser which provides simple and convenient access to json data. It does not deserialize to custom objects but simply parses the data into a hierarchical structure.


Parsing your example with SimpleJSON you can just do:

 JSONNode data = JSON.Parse(yourJsonText);
 string range = data["range"].Value;
 string majorDimension = data["majorDimension"].Value;
 foreach(JSONNode n in data["values"])
 {
     string key = n[0].Value;
     string vEngGB = n[1].Value;
 }

Note that each JSONNode provides a struct enumerable / enumerator of type KeyValuePair<string, JSONNode>. However SimpleJSON comes with an implicit conversion operator that allows to convert KeyValuePair<string, JSONNode> into JSONNode. This happens in my example with the foreach loop. However you can also iterate through the "arrays" in the classical way.

 JSONNode values = data["values"];
 for(int i = 0; i < values.Count; i++)
 {
     string key = values[i][0].Value;
     string vEngGB = values[i][1].Value;
 }



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

OpenFilePanel alternative 1 Answer

Loading JSON into JSON issue,JSON file inside JSON problem 0 Answers

String Parsing 1 Answer

Instantiate a prefab from jsonarray? 1 Answer

Creating a json file with a multidimensional array,Multi dimensional array in json 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