json文件怎么转换成html格式

json文件怎么转换成html格式,第1张

import java.io.BufferedReader

import java.io.File

import java.io.FileInputStream

import java.io.FileNotFoundException

import java.io.FileOutputStream

import java.io.FileWriter

import java.io.IOException

import java.io.InputStream

import java.io.InputStreamReader

/**

* java读写文件

* 读取d:/1.txt文件内容,写入f:/text.txt文件中.

*

* 写入文件换行用fw.write("\r\n")

* 或者fw.write("\n")

* @author young

*

*/

public class FileWriterTest {

// 读写文件

public static void rwFile(){

FileWriter fw = null

BufferedReader br = null

try {

// 定义FileWriter对象,关联文件f:\text.txt,用来向文件写内容

fw = new FileWriter("f:\\text.txt", true)

// 定义bufferedReader对象,用来读取d:\1.txt文件内容

br = new BufferedReader(new InputStreamReader(

new FileInputStream("d:\\1.html"), "UTF-8"))

String line = null

// 每次读取一行内容,循环读取,读到文件末尾结束

while ((line = br.readLine()) != null) {

System.out.println("文件内容: " + line)

fw.write(line)

// 刷新缓冲流,

fw.flush()

}

// 关闭I/O流

br.close()

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

} finally {

if (fw != null) {

try {

fw.close()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}

}

public static void main(String[] args) {

rwFile()

}

}

function jsonclick() {

            var jsonstr = '{"desc":"OK","status":1000,"data":{"wendu":"34","ganmao":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。","forecast":[{"fengxiang":"东风","fengli":"3-4级","high":"高温 35℃","type":"多云","low":"低温 27℃","date":"22日星期一"},{"fengxiang":"东风","fengli":"微风级","high":"高温 34℃","type":"多云","low":"低温 26℃","date":"23日星期二"},{"fengxiang":"东风","fengli":"微风级","high":"高温 35℃","type":"多云","low":"低温 27℃","date":"24日星期三"},{"fengxiang":"东风","fengli":"微风级","high":"高温 34℃","type":"多云","low":"低温 26℃","date":"25日星期四"},{"fengxiang":"东北风","fengli":"3-4级","high":"高温 33℃","type":"阴","low":"低温 25℃","date":"26日星期五"}],"yesterday":{"fl":"3-4级","fx":"北风","high":"高温 33℃","type":"雷阵雨","low":"低温 27℃","date":"21日星期日"},"aqi":"53","city":"常州"}}'

            var list = new Function("return" + jsonstr)()

            //html代码随便拼凑成需要的格式就行了

            var htmlstr = "<ul style='border:1px solid #aaccff height:100px width:600px'> <li> 状态码:"

                + list.status + "</li> <li> 日期:"

                + list.data.yesterday.date + "</li> <li> 地址:"

                + list.data.city + "</li> <li> 预测风向:"

                + list.data.forecast[0].fengxiang + "</li> </ul>"

            document.getElementById("id").innerHTML = htmlstr

        }

HTML5已原生支持json的解析,window.JSON.parse()将json格式字符串转换为json对象,window.JSON.stringify()将json对象转换为json格式字符串。

示例:

Html代码 

<!DOCTYPE HTML>  

<html>  

<head>  

    <title>Window.JSON</title>  

    <meta charset="gb18030">  

</head>  

  

<body>  

    <button type="button" id="btn1">解析json字符串</button>  

    <button type="button" id="btn2">json对象转换为json字符串</button>  

    <div id="res">  

      

    </div>  

    <script language="JavaScript">  

    <!--  

        var jsonStr = "{\"total\":100,\"data\":[{\"id\":10001,\"name\":\"scott\"},{\"id\":10002,\"name\":\"tiger\"}]}"  

        var jsonObj = window.JSON.parse(jsonStr)  

          

        document.getElementById("btn1").onclick = function() {  

            var str = "json字符串解析为json对象<br>"  

            str += "<span>Total:"+jsonObj.total+"</span><br><span>Data:"  

            for (var i=0i<jsonObj.data.length  i++)  

            {  

                str += "id:" + jsonObj.data[i].id + ",name:" + jsonObj.data[i].name+"<br>"  

            }  

            str += "</span><br>"  

            document.querySelector("#res").innerHTML = str  

  

        }  

        document.getElementById("btn2").onclick = function() {  

            var jsonObj = {total:100,data:[{id:10001,name:"scott"},{id:10002,name:"tiger"}]}  

            var jsonStr = window.JSON.stringify(jsonObj)  

            var str = "转为json字符串:<br>" + jsonStr  

            document.querySelector("#res").innerHTML = str  

        }  

    //-->  

    </script>  

</body>  

</html>

单击“解析json字符串”按钮,结果:

json字符串解析为json对象

Total:100

Data:id:10001,name:scott

id:10002,name:tiger

单击“json对象转换为json字符串”按钮,结果:

转为json字符串:

{"total":100,"data":[{"id":10001,"name":"scott"},{"id":10002,"name":"tiger"}]}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6163241.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-16
下一篇 2023-03-16

发表评论

登录后才能评论

评论列表(0条)

保存