当前位置:首页>开发>正文

js文件(里面是json数组)提取和读写问题 html处理php返回的json数组的问题。

2023-05-27 10:55:07 互联网 未知 开发

 js文件(里面是json数组)提取和读写问题 html处理php返回的json数组的问题。

js文件(里面是json数组)提取和读写问题

javascript 读写文件,参考例子如下:
<html>  
<head>  
<title>JS操作文本文件</title>  
</head>  
<body>  

<script>  
var arr = 1  
function Write2Text()  
{  
var fso = new ActiveXObject("Scripting.FileSystemObject")  
var f = fso.CreateTextFile("a.txt", true)  
f.write(arr)  
f.Close()  
}  
</script>  
<input type=button value="Write" onclick="Write2Text()">  

<script>  
function GetHeader(src) {   
var ForReading=1   
var fso=new ActiveXObject("Scripting.FileSystemObject")   
var f=fso.OpenTextFile(src,ForReading)   
return(f.ReadAll())   
}   
function ReadText(){  
var arr=GetHeader("a.txt").split(" ")  
for(var i=0i<arr.lengthi ){  
alert("第" (i 1) "行数据为:" arr[i])  
}  
}  
</script>  

<input type=button value="Read" onclick="ReadText()">  

</body>  
</html>

html处理php返回的json数组的问题。

你的代码没太大问题, 只要改两个地方就可以了.

修改statechange函数, 里面的xmlhttp的 h改为大写 H, 还有返回的data要用JSON.parse转化成json对象就行了

json数据中有json数组 怎么解析

实例:
import net.sf.json.JSONArray

public class TestJson
{
public static void main(String []args)
{
String json = "[{"a":"111","b":"222","c":"333"},{"a":"1000","b":"2000","c":"000"},{"a":"999","b":"300","c":"700"}]"
JSONArray jsonArr = JSONArray.fromObject(json)
String a[] = new String[jsonArr.size()]
String b[] = new String[jsonArr.size()]
String c[] = new String[jsonArr.size()]
for (int i = 0 i < jsonArr.size() i ) {
a[i] = jsonArr.getJSONObject(i).getString("a")
b[i] = jsonArr.getJSONObject(i).getString("b")
c[i] = jsonArr.getJSONObject(i).getString("c")
}

for (int i = 0 i < c.length i ) {
System.out.print(a[i] " ")
System.out.print(b[i] " ")
System.out.print(c[i])
System.out.println()
}
}
}

PHP json格式化成数组 怎么处理

        //使用json_decode将json字符串转换为数组
$json = {"Hello":"World"}
//json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
$decode_array = json_decode($json, true)
print_r($decode_array)
?>输出:Array ( [Hello] => World )

最新文章