Ⅰ 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);