I am sending data from dot net minimal API :
app.MapGet("/mytableDistinct", async ([FromServices] ShipmentDbContext dbContext) =>
{
IQueryable<long> query = dbContext.MyTables.Select(mt => mt.ShipmentID).Distinct();
var shipmentIDs = await query.ToListAsync();
return Results.Ok(new JsonResult(shipmentIDs));
});
It is showing response as following in unity:
{"contentType":null,"serializerSettings":null,"statusCode":null,"value":[4739167,4745212]}
But while parsing this data, it is failing and I am not sure what is wrong. Parsing code:
var request = UnityWebRequest.Get(url);
var operation = request.SendWebRequest();
while (!operation.isDone)
{
await Task.Yield();
}
if (request.result == UnityWebRequest.Result.Success)
{
//var jsonResponse = request.downloadHandler.text;
//var apiResponse = JsonUtility.FromJson<AllShipmentIDs>(jsonResponse);
try
{
var jsonResponse = request.downloadHandler.text;
Debug.Log(jsonResponse);
var apiResponse = JsonUtility.FromJson<AllShipmentIDs>(jsonResponse);
List<int> shipmentIDs = apiResponse.Value;
Debug.Log("Shipment IDs: " + string.Join(",", shipmentIDs));
}
catch (Exception ex)
{
Debug.LogError($"Failed to deserialize API response: {ex.Message}");
}
}
The values are always null. This is object model:
[System.Serializable]
public class AllShipmentIDs
{
public string ContentType { get; set; }
public object SerializerSettings { get; set; }
public int? StatusCode { get; set; }
public List<int> Value { get; set; } = new List<int>();
}
If the above 'Value' is not initialized, it shows, object reference is not set to an instance of object, error.
I tried lowercase and CamelCase but nothing seems to make it parse successfully. On the same script I can parse other objects from another endpoint reposes. What should I do?