2016-12-11 21 views
-1

我是asp.net MVC中的新成員,我想解析JSON數據,所以我做了一些研究如何做到這一點,並根據這一點做了,但無法獲得結果。在asp mvc中無法獲得Json數據解析C#

我使用嘗試捕捉收到此錯誤

'長' 不包含 'listDemo'

JSON數據的定義:存儲在var response

{ 
    "RequestNo" : 232, 
    "CardDetail" : { 
     "Mobileno" : 98983232323, 
     "Balance" : 0, 
     "RemitLimitAvailable" : 2323 
    }, 
    "listDemo" : { 
     "TAG0" : { 
      "Code" : 2323, 
      "Name" : Demo, 
      "Type" : Demo 
     }, 
     "TAG1" : { 
      "Code" : 424, 
      "Name" : Demo, 
      "Type" : Demo 
     } 
    }, 
    "Response" : SUCCESS, 
    "Message" : REQUEST SUCCESSFULLY COMPLETED., 
    "Code" : 300 
} 

C#代碼:

dynamic dataObj = JsonConvert.DeserializeObject(response); 
try 
{ 
    string benCode = dataObj.listDemo.TAG0.Code; 
} 
catch (Exception ex) 
{ 
    table += ex.Message; 
} 

有什麼方法可以獲取數據嗎?

回答

1

string benCode = dataObj.Beneficiary.TAG0.Code;

我沒有發現在你的JSON任何Beneficiary ...

使用

int benCode = dataObj.listDemo.TAG0.Code; 

(listDemo.TAG0.Code也返回一個整數,而不是字符串)

備註:有效的JSON示例爲

{ 
    "RequestNo" : 232, 
    "CardDetail" : { 
     "Mobileno" : 98983232323, 
     "Balance" : 0, 
     "RemitLimitAvailable" : 2323 
    }, 
    "listDemo" : { 
     "TAG0" : { 
      "Code" : 2323, 
      "Name" : "Demo", 
      "Type" : "Demo" 
     }, 
     "TAG1" : { 
      "Code" : 424, 
      "Name" : "Demo", 
      "Type" : "Demo" 
     } 
    }, 
    "Response" : "SUCCESS", 
    "Message" : "REQUEST SUCCESSFULLY COMPLETED.", 
    "Code" : 300 
} 
+0

我做到了,但仍然有同樣的問題。 –

+1

@ManishTiwari你爲什麼要改變你發佈的原始代碼? – Jim

+1

@ManishTiwari ...和btw你的錯誤*'長'不包含'listDemo'的定義*是無法驗證的。所以考慮製作一個[Minimal,Complete,and Verifiable example](http://stackoverflow.com/help/mcve) – Jim