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 atom182 · Feb 27, 2018 at 12:42 AM · json

How to write and read Json in Unity

i have a problem with unity and Json. Well i need to save a name and score from a game, i think Json it's a great option but i never used them.

I understand the basics, how to read one JsonObject but the problem i don't know how to creat a new object.

later i try to create an array of object in Json and i dont know hoy read them.

this is my idea.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 
 
 public class Score : MonoBehaviour {
     string path;
     string jsonString;
     // Use this for initialization
     void Start () {
         path = Application.dataPath + "/ScoreRecords.json";
         jsonString = File.ReadAllText (path); 
         ListaRecords listaRecords = JsonUtility.FromJson<ListaRecords> (jsonString);
         print(listaRecords);
         foreach(Record record in listaRecords){
             Debug.Log ("nombre: " + record.name + "score: " + record.score);
         }
 
     }
 
     // Update is called once per frame
     void Update () {
 
     }
 }
 
 
 [System.Serializable]
 public class Record{
     public string name;
     public int score;
 }
 
 [System.Serializable]
 
 public class ListaRecords{
     public List<Record> Records;
 }

and this is my Json

 {
     "Records": [
         {"nombre":"daniel", "puntos":100},
         {"nombre":"Adrian", "puntos":200}
     ]
 }
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
2
Best Answer

Answer by Bunny83 · Feb 27, 2018 at 01:06 AM

Unity's JsonUtility is an object mapper. That means the names of the fields have to match the keys in your json data. So if you you want to go with your JSON data you should change your Record class to:

 [System.Serializable]
 public class Record{
     public string nombre;
     public int puntos;
 }

If you want to keep the code you have to change your JSON to

 {
     "Records": [
         {"name":"daniel", "score":100},
         {"name":"Adrian", "score":200}
     ]
 }

If you want more flexibility you may want to use a more generic JSON framework like my SimpleJSON. With it you can directly parse and access the JSON data like this:

 jsonString = File.ReadAllText (path); 
 JSONNode data = JSON.Parse(jsonString);
 foreach(JSONNode record in data["Records"])
 {
     Debug.Log ("nombre: " + record["nombre"].Value + "score: " + record["puntos"].AsInt);
 }
 


Comment
Add comment · Show 5 · 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 frederic_unity363 · Feb 01, 2019 at 02:28 AM 0
Share

Do I need an extra library or class to use JSON.Parse? I only have Jsonutility with FromJSON. Does it work similarly?

avatar image Bunny83 frederic_unity363 · Feb 01, 2019 at 02:56 AM 1
Share

Uhm, have you actually read my answer? There's a link above the code snippet where i used JSON.Parse and i clearly mentioned my SimpleJSON framework

avatar image frederic_unity363 Bunny83 · Feb 01, 2019 at 03:04 AM 0
Share

... I must have been blind

avatar image alarm656 · Jan 02, 2020 at 08:51 PM 0
Share

You dont need use or include SimpleJSON framework

just use foreach (JSONNode record in data[Record]) { // do your stuff here }

avatar image Bunny83 alarm656 · Jan 02, 2020 at 10:38 PM 0
Share

I'm not sure what you're talking about. $$anonymous$$y SimpleJSON framework is not part of Unity. So you have to have a copy of the SimpleJSON.cs file inside your project in order to use it.

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

79 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

Related Questions

my localization loader only load one text's value 0 Answers

iOS-Error: Can't use nested objects in json 0 Answers

how to write GameObject position in the Scene to json file ? 1 Answer

Deserialize Json into list.,how to deserialize a list via .FromJson 1 Answer

Parsing JsonData throws error when value is null, what to do? 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