2015-11-02 83 views
1

我有以下JSON響應我怎樣才能序列化Windows手機的JSON響應?

string Response= "" { 
"category": [ 
    { 
     "biller": [ 
      { 
       "adhocPayment": "Y", 
       "billPresentment": "N", 
       "billerCategoryId": "05", 
       "billerId": "BILLPAYTEST", 
       "billerLogo": "#", 
       "billerName": "Airtel Chennai", 
       "displayLogoName": "#", 
       "label1": "CONSUMER NO", 
       "label2": "PHONE NO", 
       "maxAmount": "10000", 
       "minAmount": "10" 
      }, 
      { 
       "adhocPayment": "Y", 
       "billPresentment": "N", 
       "billerCategoryId": "05", 
       "billerId": "VODFONE", 
       "billerLogo": "#", 
       "billerName": "VODOFONE", 
       "displayLogoName": "#", 
       "label1": "CONSUMER NO", 
       "label2": "PHONE NO", 
       "maxAmount": "10000", 
       "minAmount": "10" 
      } 
     ], 
     "categoryName": "Mobile Recharge", 
     "totalRecords": 2 
    }, 
    { 
     "biller": [ 
      { 
       "adhocPayment": "N", 
       "billPresentment": "N", 
       "billerCategoryId": "30", 
       "billerId": "SUNTVDTH", 
       "billerLogo": "#", 
       "billerName": "SUN TV", 
       "displayLogoName": "#", 
       "label1": "Subscriber ID^10^10^N^N^N", 
       "label2": "#", 
       "maxAmount": "10000", 
       "minAmount": "100" 
      } 
     ], 
     "categoryName": "DTH Recharge", 
     "totalRecords": 1 
    } 
], 
"dcid": "01", 
"startRecNo": "0001", 
"totalRecord": 0, 
"txnno": "0211151051285128" 
}"" 

連載它我下面

外部類

public class RootObject 
    { 

     public List<Category> category { get; set; } 
     public string dcid { get; set; } 
     public object startRecNo { get; set; } 
     public int totalRecord { get; set; } 
     public string txnno { get; set; } 
    } 

    public class Category 
    { 
     public List<Biller> biller { get; set; } 
     public string categoryName { get; set; } 
     public int totalRecords { get; set; } 
    } 

    public class Biller 
    { 

     public string adhocPayment { get; set; } 
     public string bankCode { get; set; } 
     public string billPresentment { get; set; } 
     public string billerCategoryId { get; set; } 
     public string billerId { get; set; } 
     public string billerLogo { get; set; } 
     public string billerMnemoName { get; set; } 
     public string billerName { get; set; } 
     public string delChannelId { get; set; } 
     public string label1 { get; set; } 
     public string label2 { get; set; } 
     public string label3 { get; set; } 
     public string label4 { get; set; } 
     public string label5 { get; set; } 
     public string labelType { get; set; } 
     public string maxAmount { get; set; } 
     public string minAmount { get; set; } 
     public string minimumPayment { get; set; } 
     public string recordCount { get; set; } 
    } 

在按鈕單擊事件處理IAM序列化這樣

foreach (var nit in rootelement.category) 
     { 

      for (int i = 1; i < nit.biller.Count; i++) 
      { 
       string b = nit.biller[i].billerName.ToString(); 
       string c=nit.biller[i].label1.ToString(); 
      } 
     } 

我的要求是有兩個作爲響應的標籤(label1,label2)..基於此,我將在設計中創建兩個文本框並顯示它們。

每次發生更改時標籤的響應數是動態的, 如何檢查/獲取標籤數從響應。 基礎上,我需要顯示文本框的設計的同號。

請幫助我對此..

+0

首先,它是一個字符串,它已經被序列化了,所以你正在尋找反序列化它。其次,你不需要手動寫這個,因爲有幾個選項可以幫你做。 .Net現有的實現:https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx和外部庫,如:http://www.newtonsoft.com/json – Dhunt

回答

0
dynamic collectionWrapper = new 
{ 
    ppp = new RootObject() 
    { 
     dcid = "01", 
     startRecNo = "0001", 
     totalRecord = 0, 
     txnno = "0211151051285128", 
     category = new List<Category> 
     { 
      new Category() 
      { 
       categoryName = "Mobile Recharge", 
       totalRecords = 2, 
       biller = new List<Biller> 
       { 
        new Biller() 
        {         
         adhocPayment="Y", 
         billPresentment="N", 
         billerCategoryId="05", 
         billerId="BILLPAYTEST", 
         billerLogo="#", 
         billerName= "Airtel Chennai", 
         displayLogoName= "#", 
         label1= "CONSUMER NO", 
         label2= "PHONE NO", 
         maxAmount= "10000", 
         minAmount= "10" 
        }, 
        ...................... 
       } 
      }, 
      new Category() 
      { 
       categoryName = "Mobile Recharge", 
       totalRecords = 2, 
       biller = new List<Biller> 
       { 
        new Biller() 
        {         
         adhocPayment="Y", 
         billPresentment="N", 
         billerCategoryId="05", 
         billerId="BILLPAYTEST", 
         billerLogo="#", 
         billerName= "Airtel Chennai", 
         displayLogoName= "#", 
         label1= "CONSUMER NO", 
         label2= "PHONE NO", 
         maxAmount= "10000", 
         minAmount= "10" 
        } 
       } 
      }, 
      ..................... 
     } 
    } 
}; 

var output = JsonConvert.SerializeObject(collectionWrapper); 
0

您可以直接使用JSON.NET反序列化到JSON和JSON反序列化。請避免「手工」操作。您可以將在一個單一的代碼行的任務:

string output = JsonConvert.SerializeObject(product); 

鋁,你需要的是available here的文檔。

您可以將它作爲Nuget package安裝在您的項目中。

實際上,您應該使用Web API,它允許您以非常簡單的方式使用HTTP和JSON序列化在設備和服務器之間進行通信。