-14

I have this JSON array, I got it from here. I want to extract from it some coins price. Here's what I want to have: BTC price: 15000$ XRP price: 3$ How can I achieve this in c# ?

[
{
    "id": "bitcoin", 
    "name": "Bitcoin", 
    "symbol": "BTC", 
    "rank": "1", 
    "price_usd": "15235.5", 
    "price_btc": "1.0", 
    "24h_volume_usd": "17910600000.0", 
    "market_cap_usd": "255647119350", 
    "available_supply": "16779700.0", 
    "total_supply": "16779700.0", 
    "max_supply": "21000000.0", 
    "percent_change_1h": "0.36", 
    "percent_change_24h": "8.91", 
    "percent_change_7d": "-3.41", 
    "last_updated": "1514994260"
}, 
{
    "id": "ripple", 
    "name": "Ripple", 
    "symbol": "XRP", 
    "rank": "2", 
    "price_usd": "2.88573", 
    "price_btc": "0.00019146", 
    "24h_volume_usd": "4799670000.0", 
    "market_cap_usd": "111790712459", 
    "available_supply": "38739144847.0", 
    "total_supply": "99993093880.0", 
    "max_supply": "100000000000", 
    "percent_change_1h": "0.88", 
    "percent_change_24h": "19.54", 
    "percent_change_7d": "120.16", 
    "last_updated": "1514994241"
}
]
  • Use newtonsoft.json to import the data into a list, then iterate over the list as you would do normally. Which bit are you stuck with? – Neil Jan 05 '18 at 15:08
  • Please do your research, there are several questions fully answered that fills the requirement of this question. – Rafael Jan 05 '18 at 15:12

2 Answers2

2

Use Newtonsoft.net for parsing of JSON data.

var obj = JsonConvert.DeserializeObject(json);

You can create class to hold the data

Class CryptoCurrencyDetails{

 public String id {get; set;}
 public String name{get; set;}
 public String symbol{get; set;}
    .
    .
    .

public String last_updated{get; set;}
}

Then you can convert your JSON array into C# list by :

List<CryptoCurrencyDetails> list = JsonConvert.DeserializeObject<List<CryptoCurrencyDetails>>(json);
Rohan Kadu
  • 1,311
  • 2
  • 12
  • 22
0

I advise you to use an object for mapping this Array, for example :

[DataContract]
public class CryptoCurrencyData
{
    [DataMember(Name ="id")]
    public int Id { get; set; }
    //One property for each Json array member
}

And then use a Json serializer like this :

using (MemoryStream myMemoryStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonArrayString)))
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CryptoCurrencyData));
    return (CryptoCurrencyData)serializer.ReadObject(myMemoryStream);
} 
SunPaz
  • 145
  • 7
  • 2
    `DataContractJsonSerializer` pales in comparison with Newtonsoft. Its use is not recommended. –  Jan 05 '18 at 15:20
  • The OP has an array, this code will only read a single item. – pstrjds Jan 05 '18 at 15:20
  • @Amy You are right, I didn't see the Xamarin tag... – SunPaz Jan 05 '18 at 15:38
  • 1
    @SunPaz that tag isn't relevant. `DataContractJsonSerializer` performs worse and had fewer features than Newtonsoft everywhere. –  Jan 05 '18 at 16:31