SkyFire101
SkyFire101

Reputation: 3

C#: How to import API Data from web?

how can i use data from these sample API in a c# script?

This is what i get from the web:

[
    {
        "id": "Item1", 
        "valueItem": "100.123456", 
    }, 
    {
        "id": "Item2", 
        "valueItem": "55.23344",     
    }
]

I simple need it to be a double like:

public double Item1 = 100.123456;

Hope you can help, thanks in advance

Upvotes: 0

Views: 108

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30685

Using Json.net you'd do

public class ItemClass
{
    public string id { get; set; }
    public double valueItem { get; set; }
}

var objList = JsonConvert.DeserializeObject<List<ItemClass>>(json);

You can use http://json2csharp.com/ to generate your base classes if you need to.

foreach(var item in objList)
{
    Console.WriteLine("Item value: " + item.valueItem.ToString());
}

Upvotes: 1

Related Questions