2

I'm trying to deserialize a Json String to a object but I only get 0 and null back.

Here is my code:

string result = "[{\"page\":1,\"pages\":1,\"per_page\":\"50\",\"total\":1},[{\"id\":\"BEL\",\"iso2Code\":\"BE\",\"name\":\"Belgium\",\"region\":{ \"id\":\"ECS\",\"value\":\"Europe & Central Asia(all income levels)\"},\"adminregion\":{ \"id\":\"\",\"value\":\"\"},\"incomeLevel\":{ \"id\":\"OEC\",\"value\":\"High income: OECD\"},\"lendingType\":{ \"id\":\"LNX\",\"value\":\"Not classified\"},\"capitalCity\":\"Brussels\",\"longitude\":\"4.36761\",\"latitude\":\"50.8371\"}]]";

var serializer = new DataContractJsonSerializer(typeof(LandRootObject));

var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));

var data = (LandRootObject)serializer.ReadObject(ms);


public class LandRootObject
{
    public int page { get; set; }
    public int pages { get; set; }
    public string per_page { get; set; }
    public int total { get; set; }

    [DataMember]
    public List<Land> land { get; set; }
}

Thanks!

3
  • "get 0 and null back"??? So What? 0 or null? I suppose the latter, as 0 is a number which is never null. Commented Jan 12, 2016 at 11:03
  • Yes, but the land variable and per_page can be null so.. ;) Commented Jan 12, 2016 at 11:19
  • When debugging what is the content of data? That was the actual question. And in particular: which properties are 0 or null? Commented Jan 12, 2016 at 11:44

3 Answers 3

1

I have tested this method and it's working.

Your entity classes. (I did not code all these classes. They are code generated using paste special.)

public class LandRootObject
{
    public int page { get; set; }
    public int pages { get; set; }
    public string per_page { get; set; }
    public int total { get; set; }
}

public class LandBodyObject
{
    public string id { get; set; }
    public string iso2Code { get; set; }
    public string name { get; set; }
    public Region region { get; set; }
    public Adminregion adminregion { get; set; }
    public Incomelevel incomeLevel { get; set; }
    public Lendingtype lendingType { get; set; }
    public string capitalCity { get; set; }
    public string longitude { get; set; }
    public string latitude { get; set; }
}

public class Region
{
    public string id { get; set; }
    public string value { get; set; }
}

public class Adminregion
{
    public string id { get; set; }
    public string value { get; set; }
}

public class Incomelevel
{
    public string id { get; set; }
    public string value { get; set; }
}

public class Lendingtype
{
    public string id { get; set; }
    public string value { get; set; }
}

Then the deserialisation method. Your Json has two parts. So I am splitting it in to 2 for deserialisation.

    string result = "[{\"page\":1,\"pages\":1,\"per_page\":\"50\",\"total\":1},[{\"id\":\"BEL\",\"iso2Code\":\"BE\",\"name\":\"Belgium\",\"region\":{ \"id\":\"ECS\",\"value\":\"Europe & Central Asia(all income levels)\"},\"adminregion\":{ \"id\":\"\",\"value\":\"\"},\"incomeLevel\":{ \"id\":\"OEC\",\"value\":\"High income: OECD\"},\"lendingType\":{ \"id\":\"LNX\",\"value\":\"Not classified\"},\"capitalCity\":\"Brussels\",\"longitude\":\"4.36761\",\"latitude\":\"50.8371\"}]]";

    var parts = result.Split(new[] {",["}, StringSplitOptions.None);
    if (parts.Length > 1)
    {
        var header = parts[0].Replace("[", "");
        var jsonHeader = JsonConvert.DeserializeObject<LandRootObject>(header);

        var body = "[" + parts[1].Replace("]]","]");
        var jsonBody = JsonConvert.DeserializeObject<List<LandBodyObject>>(body);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Use List type

var serializer = new DataContractJsonSerializer(typeof(List<LandRootObject>));
// ...
var data = (List<LandRootObject>)serializer.ReadObject(ms);

Edit: Now I see - your json array consists of 2 different elements. I suppose its [RootObject, Lands]. You better use the Newtonsoft.Json to deserialize the object.

var str = "[{\"page\":1,\"pages\":1,\"per_page\":\"50\",\"total\":1},[{\"id\":\"BEL\",\"iso2Code\":\"BE\",\"name\":\"Belgium\",\"region\":{ \"id\":\"ECS\",\"value\":\"Europe & Central Asia(all income levels)\"},\"adminregion\":{ \"id\":\"\",\"value\":\"\"},\"incomeLevel\":{ \"id\":\"OEC\",\"value\":\"High income: OECD\"},\"lendingType\":{ \"id\":\"LNX\",\"value\":\"Not classified\"},\"capitalCity\":\"Brussels\",\"longitude\":\"4.36761\",\"latitude\":\"50.8371\"}]]";
var arr = JArray.Parse(str);
var rootJson = arr.ElementAt(0).ToString();
var root = JsonConvert.DeserializeObject<LandRootObject>(rootJson);
var landsJson = arr.ElementAt(1).ToString();
root.Lands = JsonConvert.DeserializeObject<List<Land>>(landsJson);

Comments

0

tryto change the above code to following

   ms.Position = 0;   // change only this line
   var data = (LandRootObject)serializer.ReadObject(ms);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.