Objective
Convert Received JSON from API to useful C# data structure
Overview
Process involved with the following:
- Using online tool to convert the json schema into C# data structure
- Save the C# data structure as DTO class
- Convert the JSON Data to DTO class
- Accessing the DTO data
Example JSON Data
{
"code": "200",
"msg": "the results is:",
"result": [
{
"Position": "{\"address\":\"Opp Seletar Airport, Singapore\",\"lat\":1.4208723379839088,\"lng\":103.86265088778731,\"zoom\":18,\"place_id\":\"ChIJeVt1C0oU2jERl9Y4zZM8jdg\",\"city\":\"Singapore\",\"country\":\"Singapore\",\"country_short\":\"SG\"}",
"coin_value": "1",
"desc": "Dino Testing File",
"object_url": "https://mergingreality.co/wp-content/uploads/2021/08/dino.coinobj",
"lat": 1.4208723379839088,
"lng": 103.86265088778731
},
{
"Position": "{\"address\":\"104 Taman Permata, Macritchie Petai Trail, Singapore 575227\",\"lat\":1.3478748075656783,\"lng\":103.83246180963138,\"zoom\":14,\"place_id\":\"ChIJIRozPoQX2jERs3dV7RVwBSQ\",\"street_number\":104,\"street_name\":\"Taman Permata\",\"city\":\"Singapore\",\"post_code\":575227,\"country\":\"Singapore\",\"country_short\":\"SG\"}",
"coin_value": "1",
"desc": "this is a merlion",
"object_url": "https://mergingreality.co/wp-content/uploads/2021/09/YWS.fbx",
"lat": 1.3478748075656783,
"lng": 103.83246180963138
}
]
}
Convert JSON TO C# Data Structure
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Result
{
[JsonProperty("Position")]
public string Position { get; set; }
[JsonProperty("coin_value")]
public string CoinValue { get; set; }
[JsonProperty("desc")]
public string Desc { get; set; }
[JsonProperty("object_url")]
public string ObjectUrl { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
[JsonProperty("lng")]
public double Lng { get; set; }
}
public class Root
{
[JsonProperty("code")]
public string Code { get; set; }
[JsonProperty("msg")]
public string Msg { get; set; }
[JsonProperty("result")]
public List<Result> Result { get; set; }
}
``
## Save the C# Data structure as DTO class
1. Create a DTO class
2. Using system and generic collection
3. add the namespace
4. changing the root to the DTO class name
5. add [Serializable] to the fields
![2021-10-01_08-27-16](media/16330464584609/2021-10-01_08-27-16.png)
Completed code:
```csharp
using System;
using System.Collections.Generic;
namespace Assets.Scripts.Models.CoinObj
{
[Serializable]
public class CoinObjByEventResultDTO
{
public string code { get; set; }
public string msg { get; set; }
public List<Result> Result { get; set; }
}
[Serializable]
public class Result
{
public string Position { get; set; }
public string coin_value { get; set; }
public string desc { get; set; }
public string object_url { get; set; }
public double lat { get; set; }
public double lng { get; set; }
}
}
Use the C# Data
CoinObjByEventResultDTO coinObjByEventResult = JsonConvert.DeserializeObject<CoinObjByEventResultDTO>(response.DataAsText);
Debug.Log(coinObjByEventResult.Result[0].lat.ToString());