【问题标题】:How to read JSON and Display in html table in c#?如何在 C# 的 html 表格中读取 JSON 和显示?
【发布时间】:2014-09-08 03:43:14
【问题描述】:

我有关注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 格式。(独立于内部键。我的意思是每一行都会按照上面的评论从新开始。

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

我只需要两列的输出,只需要 Key:Value 作为输出。

【问题讨论】:

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


    【解决方案1】:

    解决办法如下:

    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>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      • 2019-08-17
      • 2020-12-02
      • 2011-11-06
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多