2014-07-17 20 views
0

我有以下JSON如何閱讀JSON和顯示在C#中的HTML表?

{"ios_info":{"serialNumber":"F2LLMBNJFFFQ1","imeiNumber":"0138840041323551","meid":"","iccID":"89014104276400960452","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":"F2LLMBNJFFFQ1","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字符串:

private string GetKeyValuePairs(string jsonString) 
    { 
     rowcnt++; 
     var resDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString); 
     string sdict = string.Empty;/*@"<table id='tblAppleResult' cellspacing='0' cellpadding='3' border='1' style='width: 100%; border-collapse: collapse;'> 
          <tr> 
           <td> 

           </td> 
           <td> 

           </td> 
          </tr>";*/ 
     foreach (string key in resDict.Keys) 
     { 
      sdict += "<br/> " + key + " : " + (resDict[key].GetType() == typeof(Newtonsoft.Json.Linq.JObject) ? GetKeyValuePairs(resDict[key].ToString()) : resDict[key].ToString()); 
      //sdict += "<tr><td> " + key + "</td> " + (resDict[key].GetType() == typeof(Newtonsoft.Json.Linq.JObject) ? "<td>" + GetKeyValuePairs(resDict[key].ToString()) + "</td></tr>" : "<td>" + resDict[key].ToString() + "</td></tr>"); 
     } 
     //sdict += "</table>"; 
     return sdict; 
    } 

這將顯示數據。但我想在HTML Table格式(獨立內部鑰匙我的意思是每行將開始在新的按上述評論

問題:。 作爲其遞歸那張顯示錶到另一個表,即兒童。與其他孩子的屬性屬性

我需要的輸出正好兩隻與鍵列:。值作爲輸出

回答

0

這裏是解決方案:

string tbl = @"<table id='tblAppleResult' cellspacing='0' cellpadding='3' border='1' style='width: 100%; border-collapse: collapse;'><tr> 
       <td> 
        </td> 
        <td> 
        </td> 
       </tr>"; 
lblkeyValue.Text = tbl+GetKeyValuePairs(responseText)+"</table>";