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 /
This question was closed Dec 02, 2015 at 04:26 PM by laharl219 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by laharl219 · Dec 02, 2015 at 12:56 PM · c#arrayprogrammingjsonwrite data

Help using LitJson

Hello, i'm using LitJson on an app i'm currently working on.

I made a C# script wich will write a file, then i'll use the file on my app.

i can write the Json file, but not the way i want.

The way i'm making generates this output:

 {"id":10,"texto":"OIOI","link":"Link","slug":"slug_","sub":"sub","nomedocampo":"nome","boolcheck":"ok"}

I wanted it to be an array, having the "[]" on each side of the Json file like this:

 [{"id":10,"texto":"OIOI","link":"Link","slug":"slug_","sub":"sub","nomedocampo":"nome","boolcheck":"ok"}]


and here is my script wich writes the file:

 using LitJson;
 using System.IO;
 using UnityEngine;
 using System.Text;
 
 
 
 
 public class Faz_Campos : MonoBehaviour
 {
 
     public int id = 10;
     string texto = "OIOI";
     string link = "Link";
     string slug = "slug_";
     string sub = "sub";
     string nomedocampo = "nome";
     string boolcheck = "ok";
 
     public ECampos Ecampos = new ECampos(10, "OIOI", "link", "Slug_", "Sub", "nomedocampo", "ok");
     JsonData EcamposJson;
 
     void Start()
     {
 
         Ecampos = new ECampos(id, texto, link, slug, sub, nomedocampo, boolcheck);
         EcamposJson = JsonMapper.ToJson(Ecampos);
         File.WriteAllText(Application.persistentDataPath + "/StreamingAssets/campoos.json", EcamposJson.ToString());
 
 
     }
 
 }
 
 
 public class ECampos //define um campo
 {
     public int id { get; set; }
     public string texto { get; set; }
     public string link { get; set; }
     public string slug { get; set; }
     public string sub { get; set; }
     public string nomedocampo { get; set; }
     public string boolcheck { get; set; }
 
     public ECampos(int ID, string Texto, string Link, string Slug, string Sub, string Nomedocampo, string Boolcheck)
     {
         this.id = ID;
         this.texto = Texto;
         this.link = Link;
         this.slug = Slug;
         this.sub = Sub;
         this.nomedocampo = Nomedocampo;
         this.boolcheck = Boolcheck;
     }
 }
 

How do i make it work to write everything as an array?

Comment
Add comment · Show 2
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 fafase · Dec 02, 2015 at 01:22 PM 0
Share

I came up with a JSON serializer since Unity is not yet providing one.

https://unitygem.wordpress.com/json-serializer/

You need to add some attributes on the item you want to serialize and then you can use it like this:

   ECampos campos = new ECampos();
   // change some values on campos
   JSONSerialize.Serialize(Path.Combine(Application.dataPath, "campos.json"), campos);

it takes care of any json type (object, item or array). It does not consider types like Quaternion or Rect which can be converted to Vector4...

avatar image laharl219 fafase · Dec 02, 2015 at 04:25 PM 0
Share

Oh, that's nice, Thank you sir ;D

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Dave-Carlile · Dec 02, 2015 at 01:09 PM

Either put the object in an array and serialize the array... or add the brackets yourself?

 "[" + EcamposJson.ToString() + "]";

How do you plan on loading the data back? It will have to be deserialized to an array if you save it as an array.

Comment
Add comment · Show 1 · 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 laharl219 · Dec 02, 2015 at 04:26 PM 0
Share

oh, simple as that.... It worked, thank you ;D

Follow this Question

Answers Answers and Comments

37 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

Related Questions

Multiple Cars not working 1 Answer

C# Convert json arrays to unity arrys 3 Answers

C# Converting json arrays using JsonUtility, 1 Answer

Importing from an external .txt file 2 Answers

Index Out of Range Exception - can't figure it out 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