0

I get data from api from remote service:

 HttpClient client = new HttpClient();
 HttpResponseMessage response = await client.GetAsync("https://api.coinmarketcap.com/v1/ticker/?limit=1");
 HttpContent content = response.Content;

And i have two ways to get data from response:

1)Read data as String

var readContent = await content.ReadAsStringAsync();

2)and Deserialize it:

var desObject = JsonConvert.DeserializeObject>(readContent);

As you see i can Deserialize data just as List< Currency >, and i have the same errors on first and second variant:

Unmapped members were found

Now i do it using foreach loop, but i thing what it's not good idea:

foreach (var i in desObject)
{
     //currency.Id = i.Id;
     currency.Last_Updated = i.Last_Updated;
     currency.Market_Cap_Usd = i.Market_Cap_Usd;
     currency.Name = i.Name;
     currency.Percent_Change_1h = i.Percent_Change_1h;
     currency.Percent_Change_24h = i.Percent_Change_24h;
     currency.Percent_Change_7d = i.Percent_Change_7d;
     currency.Price_Btc = i.Price_Btc;
     currency.Price_Usd = i.Price_Usd;
     currency.Rank = i.Rank;
     currency.Symbol = i.Symbol;
     currency.Total_Supply = i.Total_Supply;
     currency.Volume_24h_Usd = i.Volume_24h_Usd;
}

Please tell is it possible to use Automapper in my situation, if no how i can to do it better ?

Uladz Kha
  • 2,154
  • 4
  • 40
  • 61
  • Possible duplicate of [Auto Mapper Unmapped members were found](https://stackoverflow.com/questions/27359999/auto-mapper-unmapped-members-were-found) – Tao Gómez Gil Jan 22 '18 at 21:51

2 Answers2

1

This error comes when you have not configured the automapper properly for your classes: source and the destination classes. So try this to configure automapper

MapperConfiguration mapConfig = new MapperConfiguration(cfg => {
    cfg.CreateMap<source, destination>();
});
IMapper mapper = mapConfig.CreateMapper();
mapper.Map(sourceObject, destinationObject);
Harun Diluka Heshan
  • 1,155
  • 2
  • 18
  • 30
1

It looks have no issue here with JsonCovert, can you post full code?

Updated: You're trying to insert a new currency(s) to your database with CMC API result (return as the array).

Example as you want to map List to a Class:

//Initialize Mapper
var currencies = new List<Currency>(); // Get from CMC api.
Mapper.Initialize(config =>
{
     config.CreateMap<List<Currency>, Currency>()
                 .ForMember(d => d.VolumeIn24h, o => o.ResolveUsing(s => s.FirstOrDefault()?.VolumeIn24h));
});

var currency = Mapper.Map<Currency>(currencies);

Other solution if you want:

class CurrencyJson
{
     public string Id { get; set; }

     [JsonProperty("24h_volume_usd")]
     public string Volume_24h_Usd { get; set; }
     ...
}

class Currency
{
    public string Id { get; set; }

    public string VolumeIn24h { get; set; }
}

async Task GetBitcoin()
{

    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("https://api.coinmarketcap.com/v1/ticker/?limit=1");
    HttpContent content = response.Content;
    //Read responce as String
    var readContent = await content.ReadAsStringAsync();
    //Deserialize string
    var desObject = JsonConvert.DeserializeObject<List<CurrencyWithStringProp>>(readContent);

    Mapper.Initialize(config =>
            {
                config.CreateMap<CurrencyJson, Currency>()
                       .ForMember(d => d.VolumeIn24h, o => o.MapFrom(s => s.Volume_24h_Usd));
            });

     var currencies = Mapper.Map<IList<Currency>>(currencyJsons);         
}
ocrenaka
  • 192
  • 2
  • 8