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 /
  • Help Room /
avatar image
0
Question by DragonPrinces · May 27, 2016 at 08:05 AM · json

Unity5.3 Json how to construct a complex Json object using unities new Json utility

hey guys. I have a 2 part question. First i am trying to write the code that can write a json file that looks like this.

 { 
 "Restaurants":[
      {
        "Name" : "Asami",
        "ID" : 0,
        "Plates" :
        [
          {
            "title" : "Plate1 ",
            "color" : "Red ",
            "price" : 5
          },
          {
            "title" : "Plate1 ",
            "color" : "Green ",
            "price" : 6
          },
          {
            "title" : "Plate1 ",
            "color" : "Blue ",
            "price" : 7
          },
          {
            "title" : "Plate1 ",
            "color" : "Yellow ",
            "price" : 8
          }
        ]
      }
   ]
 }

The code i have sofar is this.

 using System;
 using System.Collections;
 using System.IO;
 using UnityEngine;
 using System.Collections.Generic;

 public class UnityJsonTest : MonoBehaviour {

 public string aName;
 public int aId = 0;
 public string aPlate1, aPlate2;
 public Color aColor1, aColor2;
 public int aPrice1, aPrice2;

 // Use this for initialization
 void Start ()
 {
     Restaurant1 rester = new Restaurant1();

     rester.MainName = aName;
     rester.Character1[0].Name = aName;

     string rjson = JsonUtility.ToJson(rester);

     File.WriteAllText(Application.dataPath + "/player.json", rjson.ToString());

     MyClass myObject = new MyClass();
     myObject.level = 1;
     myObject.playerName = "Dr Charles Francis";

     string json = JsonUtility.ToJson(myObject);

     File.WriteAllText(Application.dataPath + "/test.json", json.ToString());
 }
 
 // Update is called once per frame
 void Update () {
 
 }
 }

 [Serializable]
 public class MyClass
 {
 public int level;
 public string playerName;
 }

 [Serializable]
 public class Restaurant1
 {
 public string MainName;
 public Character1[] Character1;
 }

 [Serializable]
 public class Character1
 {
 public string Name;
 public int Id;
 public Plate1[] Plate1;
 }

 [Serializable]
 public class Plate1
 {
 public string Title;
 public Color Color;
 public int Price;
 }

if you look at the MyClass myObject = new MyClass(); and the 2 lines after that. it works perfectly. That is the example from the Unity Docs.

but when i tried to use that and make it abit more complex i seem to have missed something.

in the line

 Restaurant1 rester = new Restaurant1();

 rester.MainName = aName; //This works.

 rester.Character1[0].Name = aName; 

// This does NOT work. in unity i get a "NullReferenceException: Object reference not set to an instance of an object" and i have absolutely no idea why. What am i doing wrong here?

The second part of my question is what is the best way to write this thing so that i can set the number of "Plates" objects to a user defined amount? and then i would assume i need a for loop to fill in each of the plate objects?

I have searched far and wide for examples on this but all the examples are done like the myClass. very simple with no explanation on how to build something more complex.

If you guys could help that would be amazing :)

Thanks in advance.

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 MohammadAlkhalailah · Apr 24, 2020 at 03:30 PM 0
Share

Hello Did you solve the problem. I face same issue.

0 Replies

· Add your reply
  • Sort: 

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

59 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

Related Questions

Deserialize Json into Object 0 Answers

Making a POST request to Google OAuth using WWWForms 1 Answer

Unity imposed recursion depth of Json data? 0 Answers

Read and Write Textfile on mobile (C# Unity ) 0 Answers

gameObject from jso 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