Aleks Kpur Aleks Kpur - 4 months ago 15
JSON Question

C# Adding string into listview, causes error?

So I have an piece of code here:

dynamic getPoints = JsonConvert.DeserializeObject<dynamic>(Profile.Profile.Currency.ToString());
var Points = getPoints[1].amount;
addItems(Username, Password, Points);


addItems Void

info.Text = Username;
info.SubItems.Add(Password);
info.SubItems.Add(Points);
listView1.Items.Add(info);


Problem:

So after I call this & it tries to do addItems, then this error appears:


The best overloaded method match for ProgramTest.Form1.addItems(string, string string) has some invalid arguments.


How could I solve this issue?

Answer

AddItems() doesn't take a dynamic. Solution: don't use dynamic and instead properly convert your JSON data into real types. Use JToken.Parse(). For example:

var json = JArray.Parse(Profile.Profile.Currency.ToString());
var Points = json[1].Value<string>("amount");
Comments