How to read JSON and Display in html table in c#?

∥☆過路亽.° 提交于 2019-12-25 02:58:57

问题


I have following 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":""}}}

Reading JSON string:

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

This displays the Data. But i want in HTML Table Format.(independent of inner Keys. i mean every row will began in new as per commented above.

Problem: As its recursive it goes on displaying table into another table. i.e for child attributes with other child attributes.

I need the output just in TWO COLUMNS ONLY with Key:Value as output.


回答1:


Here is the solution:

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


来源:https://stackoverflow.com/questions/24800664/how-to-read-json-and-display-in-html-table-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!