2016-01-12 123 views
2

我想反序列化一個Json字符串到一個對象,但我只得到0和null返回。JSON解析C#沒有名稱變量

這裏是我的代碼:

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; } 
} 

謝謝!

+0

「get 0 and null back」???所以呢? 0還是null?我認爲後者,'0'是一個從不'null'的數字。 – HimBromBeere

+0

是的,但陸地變量和per_page可以爲空所以..;) – NM138

+0

當調試什麼是數據的內容?這是真正的問題。特別是:哪些屬性是'0'或'null'? – HimBromBeere

回答

1

我已經測試過這個方法,它的工作。

您的實體類。 (我沒有代碼的所有這些類。它們是使用特殊粘貼生成的代碼。)

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; } 
} 

然後deserialisation方法。你的Json有兩個部分。所以我將它分解爲2進行反序列化。

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); 
    } 
1

使用List

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

編輯: 現在我明白了 - 你的JSON陣列由2個不同的元素。我想它的[RootObject, Lands]。您最好使用Newtonsoft.Json來反序列化對象。

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); 
+0

是的,我嘗試過,但後來我只是得到一個empy對象的數組.. – NM138

0

打算進軍上面的代碼更改爲以下

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