Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by sas_88 · Mar 30, 2015 at 09:31 AM · parse

Read JSON file data from server to unity c#.

How to parse JSON file which is saved in server and assign the data of JSON file to the variables declared in Unity file.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by sas_88 · Mar 31, 2015 at 04:14 AM

Used LitJson for decoding the JSON and decoded the JSON file from server.

The sample JSON file which is parsed

 {
   "title" : "Decode JSON",
   "ID" : 20,
   "buttons" :
   [
     {
       "title" : "Red ",
       "image" : "Image Url"
     },
     {
       "title" : "Green ",
       "image" : "Image Url"
     },
     {
       "title" : "Blue ",
       "image" : "Image Url"
     },
     {
       "title" : "Yellow ",
       "image" : "Image Url"
     }
   ]
 }
 

Download and add LitJson.dll to the project.

C# script to decode the JSON file

 using UnityEngine;
 using LitJson;
 using System;
 using System.Collections;
 
 public class parseJSON
 {
     public string title;
     public string id;
     public ArrayList but_title;
     public ArrayList but_image;
 }
 public class JSON_D : MonoBehaviour
 {
     // Sample JSON for the following script has attached.
     IEnumerator Start()
     {
         string url = " URL of the JSON to be Decode";
         WWW www = new WWW(url);
         yield return www;
         if (www.error == null)
         {
             Processjson(www.data);
         }
         else
         {
             Debug.Log("ERROR: " + www.error);
         }        
     }    
     private void Processjson(string jsonString)
     {
         JsonData jsonvale = JsonMapper.ToObject(jsonString);
         parseJSON parsejson;
         parsejson = new parseJSON();
         parsejson.title = jsonvale["title"].ToString();
         parsejson.id = jsonvale["ID"].ToString();
         
         parsejson.but_title = new ArrayList ();
         parsejson.but_image = new ArrayList ();
         
         for(int i = 0; i<jsonvale["buttons"].Count; i++)
         {
             parsejson.but_title.Add(jsonvale["buttons"][i]["title"].ToString());
             parsejson.but_image.Add(jsonvale["buttons"][i]["image"].ToString());
         }    
     }
 }
 
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 fsok · May 10, 2017 at 07:10 AM 0
Share

Hi @sas_88 ! I would like to know how do you do it if you only have an ip adress ins$$anonymous$$d of an url ?

Thank you !

avatar image Veith fsok · Feb 18, 2018 at 03:47 PM 0
Share

You can simply add http:// infront of your ip.

Under the hood a given domain is replaced by the ip it represents.

avatar image
10

Answer by _joe_ · Mar 30, 2015 at 10:32 AM

Use WWW to read the file from the server and save the json in a string. http://docs.unity3d.com/ScriptReference/WWW-text.html

Then use SimpleJSON to parse it and fill your variables: http://wiki.unity3d.com/index.php/SimpleJSON

Examples are provided with the Package explaining how SimpleJSON works.

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 sas_88 · Mar 30, 2015 at 11:05 AM 1
Share

Thanks@_joe.

avatar image _joe_ · Mar 30, 2015 at 11:46 AM 2
Share

Please Accept the answer if it helped you :)

avatar image
2

Answer by crevelop · Apr 22, 2021 at 07:12 PM

I put together a tutorial doing just that and a bit more.

https://www.youtube.com/watch?v=Yp8uPxEn6Vg

I use UnityWebRequest to get the JSON and the Newtonsoft.Json to deserialize it. All using async instead of coroutines.

Hope it helps :)

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
avatar image
1

Answer by ramkesh · Jun 07, 2019 at 06:37 AM

I was also facing problem in json parsing in Unity. Now i have learnt to parse json by using both Newton Json plugin and json Utility class in Unity. For both way I used json2sharp site to create class of json. With Newton json Plugin you can use same class but with json Utility you have to made little change. First you have to remove set and get and just add ; with variables. and 2nd is you have to write [Serializable] on top of all class created by json2csharp. For reference you can follow my tutorial on it. Json Parsing in Unity by using Newton soft Plugin(.Net Json) and JsonUtility Class. First I have covered Newton json and then Json Utility class.Hope it will help you.

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
avatar image
0

Answer by Vazric · Dec 27, 2016 at 10:02 PM

@sas_88 https://docs.unity3d.com/Manual/JSONSerialization.html Unity Already have builtin JSON support , enjoy it

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 mansoor090 · Dec 05, 2018 at 01:33 PM 0
Share

it is editable

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

11 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

Related Questions

Getting text from a website file and storing it in a variable 1 Answer

equation game question need help 1 Answer

How does/can Unity handle offline sync with backend? 2 Answers

Is Parse's SDK compatible with WebGL builds ? 1 Answer

Tag Manager unable to parse 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