Ⅰ php循環讀取JSON
首先你要把JSON數據轉成數組,因為在php中JSON就是一個字元串。
$jsonData='{"list":[{
"goodsid":4290,
"post":[{
"param":"uin",
"name":"賬號"
},{
"param":"goodsid",
"name":"商品ID"
},{
"param":"number",
"name":"下單數量"
}]
},';
//你的JSON數據有點亂,我復制的可能不對
$aryData=json_decode($jsonData,true);//轉碼為數組
var_mp($aryData);//列印
foreach($aryDataas$item){
var_mp($item);//列印每個條目
}
Ⅱ json 出來的數組 PHP如何取值
json_encode 是將php數組轉換為json字元串, json_decode 是將json字元串轉換為php數組。。
演示一下;
$json='{"lng":114.043784,"lat":22.575115}';
$json2Array=(array)json_decode($json);
$a=$json2Array['lng'];
$b=$json2Array['lat'];
Ⅲ PHP怎麼生成JSON列表啊
使用json_encode。假設需要轉換成json的數組為$arr,則
$json = json_encode($arr)
$json為轉換出來的json列表
Ⅳ 問大佬php中如何用 foreach解析json
首先,不推薦使用foreach進行自己去解析json字元串,因為php已經封裝了非常好用,且效率不低的內置方法,這個方法就是json_decode。
使用這個函數,可以直接把json數據轉換成數據或者對象,這個是可以控制的。轉換成對象或者數組之後,再使用foreach就可以方便遍歷得到想要的數據。
Ⅳ PHP接收json 並將接收數據插入資料庫的實現代碼
最近有一個需求,前端向後台提交json,後台解析並且將提交的值插入資料庫中,
難點
1、php解析json(這個不算難點了,網上實例一抓一大把)
2、解析json後,php怎樣拿到該拿的值
<?php
require
('connect.php');
/*
本例用到的數據:
post_array={"order_id":"0022015112305010013","buyer_id":"2","seller_id":"1","all_price":"100.00","json_list":[{"proct_id":"3","proct_number":"3"},{"proct_id":"8","proct_number":"2"},{"proct_id":"10","proct_number":"4"}]}
*/
$post_array=$_POST['post_array'];
//--解析Json,獲取對應的變數值
$obj=json_decode($post_array,TRUE);
$order_id
=
$obj['order_id'];
$buyer_id
=
$obj['buyer_id'];
$seller_id
=
$obj['seller_id'];
$all_price
=
$obj['all_price'];
$i=0;//循環變數
//--得到Json_list數組長度
$num=count($obj["json_list"]);
//--遍歷數組,將對應信息添加入資料庫
for
($i;$i<$num;$i++)
{
$list_proct_id[]=$obj["json_list"][$i]["proct_id"];
$list_proct_number[]=$obj["json_list"][$i]["proct_number"];
$insert_order_proct_sql="INSERT
INTO
tbl_order_proct
(order_id,proct_id,proct_number)
VALUES
(?,?,?)";
$result
=
$sqlconn
->
prepare($insert_order_proct_sql);
$result
->
bind_param("sss",
$order_id,$list_proct_id[$i],$list_proct_number[$i]);
$result->execute();
}
//--添加訂單信息
$insert_order_sql="INSERT
INTO
tbl_order
(order_id,buyer_id,seller_id,all_price)
VALUES
(?,?,?,?)";
$result=$sqlconn->prepare($insert_order_sql);
$result->bind_param("ssss",$order_id,$buyer_id,$seller_id,$all_price);
$result->execute();
$result
->
close();
$sqlconn
->
close();
?>
投稿者信息
昵稱:
Hola
Email:
[email protected]
Ⅵ json轉php數組中有[list]怎麼辦
1.fastjson List轉JSONArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
2.fastjson JSONArray轉List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
3.fastjson 字元串轉List
String str = "";
List<T> list = JSONObject.parseArray(str,T.class);
Ⅶ php數組輸出這樣的json
<?php
$arr_str=array(
"returncategory"=>0,
"data"=>array(
"newslist"=>array(
array(
"title"=>"標題",
"image"=>"http://192.168.1.8.8080/2014010501015450.gif",
"source"=>"我的博客",
"commentcount"=>120,
"newsid"=>10
),
),
"totalnum"=>10
),
);
echo(json_encode($arr_str));
?>
Ⅷ php json 如何正確的get
一般前端發送 ajax 的時候都有封裝好的,直接發送默認就是 json格式的數據,比如 jQuery.js的 $.ajax,$.get,$.post 等,
或者 axios 也可以。
直接把 json 字元串拼接到 url 中很容易出現問題,很可能會出現一些特殊字元沒有處理好的情況。
所以,建議直接使用成熟的庫。
Ⅸ php 如何將獲取json中的參數的值
很簡單。
因為Json實際上就是一個字元串,因此語言提供了將其轉換成數組,對象的能力。
使用json_decode就可以將字元串轉一個關聯數組。
比如$data = json_decode("{'success':1,xxxxxxxx}");
echo $data[tender_id];就OK了
Ⅹ php輸出單組json數據
首先json_encode第二參數不應該使用true
<?php
$list=[];
while($array=mysql_fetch_array($result,MYSQL_ASSOC)){
$list['hour'][]=$array["hour"];
$list['data'][]=$array["data"];
}
echojson_encode($list);