2014-07-17 49 views
0

我用JSON to C# Class Converter並生成以下類別:讀取特定的JSON字符串時發生對象引用錯誤?

JSON

{"ios_info":{"serialNumber":"F2LLMBNJFFF","imeiNumber":"01388400413235","meid":"","iccID":"8901410427640096045","firstUnbrickDate":"11\/27\/13","lastUnbrickDate":"11\/27\/13","unbricked":"true","unlocked":"false","productVersion":"7.1.2","initialActivationPolicyID":"23","initialActivationPolicyDetails":"US AT&T Puerto Rico and US Virgin Islands Activation Policy","appliedActivationPolicyID":"23","appliedActivationDetails":"US AT&T Puerto Rico and US Virgin Islands Activation Policy","nextTetherPolicyID":"23","nextTetherPolicyDetails":"US AT&T Puerto Rico and US Virgin Islands Activation Policy","macAddress":"ACFDEC6C988A","bluetoothMacAddress":"AC:FD:EC:6C:98:8B","partDescription":"IPHONE 5S SPACE GRAY 64GB-USA"},"fmi":{"@attributes":{"version":"1","deviceCount":"1"},"fmipLockStatusDevice":{"@attributes":{"serial":"F2LLMBNJFFFQ","imei":"013884004132355","isLocked":"true","isLost":"false"}}},"product_info":{"serialNumber":"F2LLMBNJFFF","warrantyStatus":"Apple Limited Warranty","coverageEndDate":"11\/25\/14","coverageStartDate":"11\/26\/13","daysRemaining":"497","estimatedPurchaseDate":"11\/26\/13","purchaseCountry":"United States","registrationDate":"11\/26\/13","imageURL":"http:\/\/service.info.apple.com\/parts\/service_parts\/na.gif","explodedViewURL":"http:\/\/service.info.apple.com\/manuals-ssol.html","manualURL":"http:\/\/service.info.apple.com\/manuals-ssol.html","productDescription":"iPhone 5S","configDescription":"IPHONE 5S GRAY 64GB GSM","slaGroupDescription":"","contractCoverageEndDate":"11\/25\/15","contractCoverageStartDate":"11\/26\/13","contractType":"C1","laborCovered":"Y","limitedWarranty":"Y","partCovered":"Y","notes":"Covered by AppleCare+ - Incidents Available","acPlusFlag":"Y","consumerLawInfo":{"serviceType":"","popMandatory":"","allowedPartType":""}}} 

從上面的JSON所有的數據被讀取,但only the line at which i get error is in reading the JSON is:

fmi":{"@attributes":{"version":"1","deviceCount":"1"},"fmipLockStatusDevice":{"@attributes":{"serial":"F2LLMBNJFFFQ","imei":"013884004132355","isLocked":"true","isLost":"false"}}}, 

錯誤:

對象引用未設置爲對象的實例。

public class AppleAPI 
{ 
    public IosInfo ios_info { get; set; } 
    public ProductInfo product_info { get; set; } 
    public Fmi fmi { get; set; } 
    public class IosInfo 
    { 
     public string serialNumber { get; set; } 
     public string imeiNumber { get; set; } 
     public string meid { get; set; } 
     public string iccID { get; set; } 
     public string firstUnbrickDate { get; set; } 
     public string lastUnbrickDate { get; set; } 
     public string unbricked { get; set; } 
     public string unlocked { get; set; } 
     public string productVersion { get; set; } 
     public string initialActivationPolicyID { get; set; } 
     public string initialActivationPolicyDetails { get; set; } 
     public string appliedActivationPolicyID { get; set; } 
     public string appliedActivationDetails { get; set; } 
     public string nextTetherPolicyID { get; set; } 
     public string nextTetherPolicyDetails { get; set; } 
     public string macAddress { get; set; } 
     public string bluetoothMacAddress { get; set; } 
     public string partDescription { get; set; } 
    } 

    public class ConsumerLawInfo 
    { 
     public string serviceType { get; set; } 
     public string popMandatory { get; set; } 
     public string allowedPartType { get; set; } 
    } 


    public class ProductInfo 
    { 
     public string serialNumber { get; set; } 
     public string warrantyStatus { get; set; } 
     public string coverageEndDate { get; set; } 
     public string coverageStartDate { get; set; } 
     public string daysRemaining { get; set; } 
     public string estimatedPurchaseDate { get; set; } 
     public string purchaseCountry { get; set; } 
     public string registrationDate { get; set; } 
     public string imageURL { get; set; } 
     public string explodedViewURL { get; set; } 
     public string manualURL { get; set; } 
     public string productDescription { get; set; } 
     public string configDescription { get; set; } 
     public string slaGroupDescription { get; set; } 
     public string contractCoverageEndDate { get; set; } 
     public string contractCoverageStartDate { get; set; } 
     public string contractType { get; set; } 
     public string laborCovered { get; set; } 
     public string limitedWarranty { get; set; } 
     public string partCovered { get; set; } 
     public string notes { get; set; } 
     public string acPlusFlag { get; set; } 
     public ConsumerLawInfo consumerLawInfo { get; set; } 
    } 

    public class Fmi 
    { 
     public Attributes invalid_nameattribute { get; set; } 
     public FmipLockStatusDevice fmipLockStatusDevice { get; set; } 
    } 
    public class FmipLockStatusDevice 
    { 
     public Attributes2 invalid_nameattribute2 { get; set; } 
    } 
    public class Attributes 
    { 
     public string version { get; set; } 
     public string deviceCount { get; set; } 
    } 

    public class Attributes2 
    { 
     public string serial { get; set; } 
     public string imei { get; set; } 
     public string isLocked { get; set; } 
     public string isLost { get; set; } 
    } 
} 

閱讀JSON:

string responseText = string.Empty; 
AppleAPI appobj = new AppleAPI(); 
responseText = appobj.VerifyAppleESN(newEsn); 
var resobj = JsonConvert.DeserializeObject<AppleAPI>(responseText.Replace("@",string.Empty)); 
lblSerialNumber.Text = resobj.product_info.serialNumber; 
. 
. 
lblappliedActivationDetails.Text = resobj.ios_info.appliedActivationDetails; 
. 
. 
//getting here error below line: Object ref notset to instance of object 
lblfmiVersion.Text = resobj.fmi.invalid_nameattribute.version; 

任何想法?

回答

1

如果你得到Object Reference not set to an instance of object.這意味着你正試圖訪問一個null對象的屬性。既然你說,它發生在這條線:

lblfmiVersion.Text = resobj.fmi.invalid_nameattribute.version; 

,這可能意味着,任何的resobjresobj.fmi,或resobj.fmi.invalid_nameattribute爲空。忽略這樣一個事實,即應該在代碼中進行適當的空檢查以幫助避免這種情況,讓我們來問一個問題,如果反序列化成功,爲什麼這些對象中的任何一個都爲空?也許一些數據畢竟沒有正確的反序列化。

使用Json.Net進行反序列化時,知道如果任何類的成員在JSON中沒有匹配的屬性是非常重要的,那麼這些成員將被Json.Net跳過並因此保留它們的默認值。 null。因此,您有一個null的可能原因是您的類中的某個屬性的名稱與JSON中的內容不匹配。

如果我們看看您的Fmi課程,立即跳出的一件事是它有一個名爲invalid_nameattribute的可疑名稱屬性。在JSON中,沒有這樣的屬性。相反,有一個名爲@attributes的房產。您的FmipLockStatusDevice類具有相同的問題。由於它們不匹配,這些屬性在反序列化過程中沒有被填充,因此它們爲空。

那麼我們該如何解決這個問題呢?

這很簡單:將[JsonProperty]屬性添加到您的類屬性,以將它們映射到正確的JSON屬性。 (當你在它,您也可以考慮改變你的C#類的東西的性質,實際上是有道理的,像「屬性」的名稱。)

public class Fmi 
{ 
    [JsonProperty("@attributes")] 
    public Attributes invalid_nameattribute { get; set; } 
    public FmipLockStatusDevice fmipLockStatusDevice { get; set; } 
} 

public class FmipLockStatusDevice 
{ 
    [JsonProperty("@attributes")] 
    public Attributes2 invalid_nameattribute2 { get; set; } 
} 

OK,現在你有一個解決方案,你應該問自己,你是如何進入這種情況的,首先你怎麼能避免它?

你說你用json2csharp.com來生成你的類。你需要知道這個工具不是萬無一失的,並且不會總是能夠生成正確的類來處理你的JSON。只要JSON屬性名稱包含標點符號或空格或以數字開頭,就是如此,因爲這些名稱不能轉換爲有效的C#屬性名稱。在這些情況下,json2csharp.com將生成以「無效」開頭的屬性名稱。您需要查找這些內容,並對您的課程進行手動調整以解決問題。不要盲目地使用生成的類並假定它們是正確的。

希望這會有所幫助。

+0

嘿@布萊恩羅傑斯!你真的很棒!你是唯一讓我知道錯誤來源的人!有了這樣一個很好的解釋詳細的解決方案!簡直太棒了!感謝您指出我「不要使用原因是因爲」,我同意說!它在第一次嘗試中起作用!謝謝+1 :) –

+0

@SHEKHARSHETE沒問題;很高興我能幫上忙。 –

相關問題