如何在c#中的html表中读取JSON和Display?

时间:2014-07-17 10:09:41

标签: c# json html-table json-deserialization

我关注JSON

{"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格式。(独立于内部键。我的意思是每一行都将按照上面的评论开始。

问题:  随着它的递归,它继续将表显示到另一个表中。即具有其他子属性的子属性。

我只需要两个列中的输出,只需使用键:值作为输出。

1 个答案:

答案 0 :(得分: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>";