-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; }
}
5
  • 6
    XML in a JSON string, really? Commented Sep 22, 2015 at 9:25
  • 3
    Have you written any code to try and achieve what you want? Commented Sep 22, 2015 at 9: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. Commented Sep 22, 2015 at 9:28
  • How far have you got? Have you been able to retrieve the JSON? Have you been able to parse the JSON? Commented Sep 22, 2015 at 9: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; } } Commented Sep 22, 2015 at 9:30

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Oh! "Paste ... As Classes" is cool.
Thanks a lot..........

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.