-2

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 "game over" screen. 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.

derHugo
  • 83,094
  • 9
  • 75
  • 115
Kaustav
  • 33
  • 6
  • 1
    https://www.newtonsoft.com/json – erikvimz Nov 07 '18 at 12:39
  • You need to _de-serialise_ your JSON into a C# class structure which matches it, then you'll have a array (or list) of tasks which you can work with just like any other list in C# (e.g. loop through it and display the content or whatever). P.S. I think you could have researched this quite easily for yourself if you'd thought a bit more about it...just googling "use json in C#" turns up a lot of good starting points. P.S. here's another good starting point: http://json2csharp.com/ - and Eric's link to JSON.NET above is also your friend. – ADyson Nov 07 '18 at 12:43
  • @ADyson I am 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. – Kaustav Nov 07 '18 at 15:24
  • Well if you use JSON.NET you can use its generic JObject and JArray classes with linq to help you do that. There are examples on the JSON.NET website. Or you could cheat and deserialise your JSON to `dynamic`. – ADyson Nov 07 '18 at 15:27
  • @ADyson Thanks !!! – Kaustav Nov 07 '18 at 15:36

1 Answers1

3

You can use JsonUtility.FromJson to create an instance of a class you define in order to store the data:

public class Task
{
    public string title ;
    public int points;
    public int completed ;
}

public class APIResponse
{
    public int current_points ;
    public Task[] tasks;
}

// In your main code

private void OnJsonResponseReceived(string jsonString)
{
    UpdateUI( JsonUtility.FromJson<APIResponse>(jsonString) ) ;
}

public void UpdateUI(APIResponse response)
{
    Debug.Log( response.current_points ) ;
    for( int i = 0 ; i < response.tasks.Length ; ++i )
    {
        Debug.LogFormat("Task '{0}' ({2} points) is {3}", response.tasks[i]., response.tasks[i]., response.tasks[i].completed ? "completed" : "not completed" ) ;
    }
}
Hellium
  • 7,206
  • 2
  • 19
  • 49
  • I am 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. – Kaustav Nov 07 '18 at 15:28
  • 2
    *`int`* `completed`? ;) – Draco18s no longer trusts SE Nov 07 '18 at 15:35
  • Unfortunately, the JSONUtility of Unity can't do this, you will have to use an external library. Check [this answer](https://stackoverflow.com/questions/20727787/deserialize-json-string-to-dictionarystring-object/20728031) to know how you can deserialize the string into a `Dictionary` – Hellium Nov 07 '18 at 16:25