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 tehmurpeh · Jul 18, 2016 at 04:57 AM · androidjson

Json not recognized on Android

So, I have this inventory system that reads in a json database and fills in item data. It's working fine in the editor but not on my android. Works fine in windows, but I have to manually go in and paste the json into the data folders. Any help on getting android to recognize this? The problem is definitely in android not finding the Json.

The top 3 commented out lines were what I was trying, but kept getting errors. I get NullReferenceException on the string content = text.ToString(); line.

 void Start()
 {
     //text = Resources.Load<TextAsset>("Items.json");
     //string content = text.ToString();
     //itemData = JsonMapper.ToObject(File.ReadAllText(content));
     itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/Resources/Items.json"));
     ConstructItemDatabase();
 }

 public Item FetchItemByID(int id)
 {
     for (int i = 0; i < database.Count; i++)
     {
         if (database[i].ID == id)
         {
             return database[i];
         }               
     }
     return null;
 }

 void ConstructItemDatabase()
 {
     for (int i = 0; i < itemData.Count; i++) 
         database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["rarity"], itemData[i]["description"].ToString(), itemData[i]["classes"].ToString(),itemData[i]["spritelocation"].ToString(),(int)itemData[i]["equipslot"], itemData[i]["prefablocation"].ToString(),(bool)itemData[i]["stackable"]));        
 }

}

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 IvovdMarel · Jul 18, 2016 at 05:33 AM

Using Dictionaries to read out JSON objects is very common but it's much easier to serialize it to a struct or class (which is also built-in to Unity using JsonUtilty.FromJson and JsonUtility.ToJson). Besides being an easier way to handle JSON, this should work on all platforms.

 void Start (){
    string jsonString = File.ReadAllText(Application.dataPath + "/Resources/Items.json")));
    MySpecificData data = JsonUtility.FromJson<MySpecificData>();
 }
 
 public class MySpecificData{
    public int id;
    public string title;
    public int rarity;
    public string description;
    public string classes;
    public string spritelocation;
    public int equipslot;
    public string prefablocation
    public bool stackable;
 }
Comment
Add comment · Show 2 · 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 tehmurpeh · Jul 18, 2016 at 10:13 AM 0
Share

Thanks for the reply! That worked, but I had to add a wrapper class for the JSON, as I read here: http://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/

Now I'm getting NullReferenceException on my ConstructItemDatabase() method.

 void ConstructItemDatabase()
 {
     for (int i = 0; i < itemData.Count; i++) 
         database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["rarity"], itemData[i]["description"].ToString(), itemData[i]["classes"].ToString(),itemData[i]["spritelocation"].ToString(),(int)itemData[i]["equipslot"], itemData[i]["prefablocation"].ToString(),(bool)itemData[i]["stackable"]));        
 }

And here's the wrapper:

public class JsonHelper { public static T[] getJsonArray(string json) { string newJson = "{ \"array\": " + json + "}"; Wrapper wrapper = JsonUtility.FromJson (newJson); return wrapper.array; }

 private class Wrapper<T>
 {
     public T[] array;
 }

}

avatar image superpig ♦♦ tehmurpeh · Jul 27, 2016 at 01:38 AM 0
Share

Try doing it without the generic. I'm not sure that would work right now.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save and load a json file on android 1 Answer

File.WriteAllText() on Android (Json file) 2 Answers

Why does JsonUtility fails miserably on Android? 1 Answer

Get Android devices current country location 0 Answers

About Android can't load json file in Application.persistentDataPath 1 Answer


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