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 KaustavDG123 · Nov 07, 2018 at 12:38 PM · wwwjsonapiwwwformwww class

Use the data received from WWW in UI

I have an API and whenever I hit it I get the following response :

{ "current_points": 2300, "tasks": [ { "title": "Fire a player", "points": 200, "completed": true }, { "title": "Buy a player", "points": 200, "completed": true }, { "title": "Press conference", "points": 1000, "completed": false }, { "title": "Set lineup", "points": 500, "completed": false }, { "title": "Win a match", "points": 200, "completed": false } ] }

Now I want to break up this data and use it to update my UI in my gameover screen. I know I have to use JSON to break this up but the problem is I don't know how to break this up so that I can get all the tasks separately. This is my first time working with API so any help will be appreciated.

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 Casiell · Nov 07, 2018 at 01:31 PM

What I always do is create a class(es) that match the response I would get from API. In your case it would look like this:

 public class Task
 {
     public string title { get; set; }
     public int points { get; set; }
     public bool completed { get; set; }
 }
 
 public class RootObject
 {
     public int current_points { get; set; }
     public List<Task> tasks { get; set; }
 }

Now just use a JSON library (I believe there is something built in Unity) to convert your JSON string to an instance of this class. When you have an instance you can just assign whatever property to any UI field you want.

For example if you want to assign total points to a text field, you would do:

 RootObject myJsonObject;
 //convert JSON to object here
 MyTextField.text = myJsonObject.current_points.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 Legend_Bacon · Nov 07, 2018 at 03:01 PM 0
Share

This is absolutely correct, although I would recommend to NOT use Unity's built-in solution, JsonUtility.


Ins$$anonymous$$d, use something like SimpleJSON. It's free, and it's way better.


Hope that helps!

Cheers,

~LegendBacon

avatar image KaustavDG123 Legend_Bacon · Nov 07, 2018 at 03:22 PM 0
Share

I am having some problems with the syntax of simpleJSON, it will be helpful if you can guide me to a tutorial of that.

I am also trying to search for a method where I can just use the nested JSON values and assign them to strings using loops without using any classes. I have seen someone do it 2-3 years back but I can't seem to recall how. If you can help me with an example of that it would be great too.

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

100 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 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

API with Key Headers required 0 Answers

How to use POST service using www class? 0 Answers

Unable to call external API (IBM Watson) via HTTP request? 1 Answer

Coroutine for WWW call not resuming 0 Answers

Calling API via proxy 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