-6

Hi All I am calling an rest service to get countries and that service is returning countries to me but it is not coming as a list or m unable to convert it to a list, Is it possible to convert the country list and bind it to a combo box. Also I want to clarify I have not created the service or I can't modify the service but I have to use that service, that is mandatory for me.

{
    "CountryList":"<Countries><Country><Code>0<\/Code><Name>aaaa<\/Name><\/Country><Country><Code>1<\/Code><Name>bbbbbb<\/Name><\/Country> ... other countries ... <\/Countries>",
    "Error":{
        "ErrorCode": 0,
        "ErrorMessage": ""
    }
}

I am using this class to parse this json

public class Error
{
    public int ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
}

public class Country
{
    public string CountryList { get; set; }
    public Error Error { get; set; }
}
newone
  • 1
  • 2
  • 6
    XML in a JSON string, really? – CodeCaster Sep 22 '15 at 09:25
  • 3
    Have you written _any_ code to try and achieve what you want? – codemonkey Sep 22 '15 at 09:26
  • @codemonkey seriously saying m new to this json and web service and unable think anything, if any can just help me to find me the ways to achieve this will be helpfull. – newone Sep 22 '15 at 09:28
  • How far have you got? Have you been able to retrieve the JSON? Have you been able to parse the JSON? – Rowland Shaw Sep 22 '15 at 09:29
  • yes and this the class m using for this public class Error { public int ErrorCode { get; set; } public string ErrorMessage { get; set; } } public class Country { public string CountryList { get; set; } public Error Error { get; set; } } – newone Sep 22 '15 at 09:30

1 Answers1

4

I don't know what kind of service returns this response, but as you see your JSON contains an XML string.

The workflow to deserialize both JSON or XML is very trivial.

Generate a couple of classes to deserialize the JSON (Visual Studio: Edit -> Paste Special -> Paste JSON As Classes):

public class Rootobject
{
    public string CountryList { get; set; }
    public Error Error { get; set; }
}

public class Error
{
    public int ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
}

And deserialize the response (using JSON.NET):

var responseObject = JsonConvert.DeserializeObject<RootObject>(jsonString);

Then you need to deserialize the XML. Create another set of classes for that (Edit -> Paste Special -> Paste XML As Classes):

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Countries
{

    private CountriesCountry[] countryField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Country")]
    public CountriesCountry[] Country
    {
        get
        {
            return this.countryField;
        }
        set
        {
            this.countryField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class CountriesCountry
{

    private string codeField;

    private string nameField;

    /// <remarks/>
    public string Code
    {
        get
        {
            return this.codeField;
        }
        set
        {
            this.codeField = value;
        }
    }

    /// <remarks/>
    public string Name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }
}

And deserialize the XML string (alternative):

var countries = Deserialize<Countries>(responseObject.CountryList);
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272