Parse de Json in C#

Asked

Viewed 221 times

-1

I’m trying to parse Json for a listbox and I’m not succeeding.

This url is an example of what I’m trying to parse... and this is what I have:

public void button1_Click(object sender, EventArgs e)
{

    dynamic m = JsonConvert.DeserializeObject(@"https://api.tibiadata.com/v2/characters/"+textBox1.Text+".json");
    string name = m.Name;
    listBox1.Items.Add(name);

}

And I have this mistake:

Newtonsoft.Json.Jsonreaderexception: 'Unexpected Character encountered while Parsing value: h. Path '', line 0, position 0.'

  • This is the website of the OS in Portuguese, translate your question

  • Your problem is that you are not passing any json/string files to Deserializeobject, you are passing only the url and no request was made to that address for information that will be deserialized.

1 answer

1


An example of a structure in C# to use the information in that file format Json, performed this example using this website

public class Guild
{
    public string name { get; set; }
    public string rank { get; set; }
}

public class LastLogin
{
    public string date { get; set; }
    public int timezone_type { get; set; }
    public string timezone { get; set; }
}

public class Data
{
    public string name { get; set; }
    public string sex { get; set; }
    public string vocation { get; set; }
    public int level { get; set; }
    public int achievement_points { get; set; }
    public string world { get; set; }
    public string residence { get; set; }
    public string married_to { get; set; }
    public Guild guild { get; set; }
    public List<LastLogin> last_login { get; set; }
    public string comment { get; set; }
    public string account_status { get; set; }
    public string status { get; set; }
}

public class Achievement
{
    public int stars { get; set; }
    public string name { get; set; }
}

public class Created
{
    public string date { get; set; }
    public int timezone_type { get; set; }
    public string timezone { get; set; }
}

public class AccountInformation
{
    public string loyalty_title { get; set; }
    public Created created { get; set; }
}

public class OtherCharacter
{
    public string name { get; set; }
    public string world { get; set; }
    public string status { get; set; }
}

public class Characters
{
    public Data data { get; set; }
    public List<Achievement> achievements { get; set; }
    public List<object> deaths { get; set; }
    public AccountInformation account_information { get; set; }
    public List<OtherCharacter> other_characters { get; set; }
}

public class Information
{
    public int api_version { get; set; }
    public double execution_time { get; set; }
    public string last_updated { get; set; }
    public string timestamp { get; set; }
}

public class RootObject
{
    public Characters characters { get; set; }
    public Information information { get; set; }
}

To carry out the deserialization I’m using Json.NET:

RootObject rObject = JsonConvert.DeserializeObject<RootObject>(json);
  • Thank you, I understand very well!

  • @Sérgioabreu Glad you understand, then put my answer as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.