json转换

json转html

{
    "DS": [
        {
            "PROVINCE": "陕西省",
            "ADMIN_CODE_CHN": "610600",
            "STATION_ID_C": "53845",
            "LON": "109.4497",
            "V31001_S": 10,
            "V31001_X": 0
        },
        {
            "PROVINCE": "陕西省",
            "ADMIN_CODE_CHN": "610600",
            "STATION_ID_C": "53845",
            "LON": "109.4497",
            "V31001_S": 10,
            "V31001_X": 0
        }
	]
}
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

public static String jsonObject2Html(JSONObject jsonObject) {
    StringBuilder htmlBuilder = new StringBuilder();
    htmlBuilder.append("");
    htmlBuilder.append("");JSONArray jsonArray = jsonObject.getJSONArray("DS");JSONObject firstObject = jsonArray.getJSONObject(0);// 创建表头
    htmlBuilder.append("");for(String key : firstObject.keySet()){
      htmlBuilder.append("");}
    htmlBuilder.append("");// 添加数据行for(int i =0; i < jsonArray.size(); i++){JSONObject dataObject = jsonArray.getJSONObject(i);
      htmlBuilder.append("");for(String key : dataObject.keySet()){
        htmlBuilder.append("");}
      htmlBuilder.append("");}

    htmlBuilder.append("
").append(key).append("
").append(dataObject.get(key)).append("
"
); return htmlBuilder.toString(); }
<style>table {border-collapse: collapse;} th, td {border: 1px solid black; padding: 8px;}</style>
<table>
<tr><th>PROVINCE</th><th>ADMIN_CODE_CHN</th><th>STATION_ID_C</th><th>LON</th><th>V31001_S</th><th>V31001_X</th></tr>
<tr><td>陕西省</td><td>610600</td><td>53845</td><td>109.4497</td><td>10</td><td>0</td></tr>
</table>

json转换_第1张图片

你可能感兴趣的:(Json,json,java)