从php中获取json的值 [英] get value from json in php

查看:63
本文介绍了从php中获取json的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中有一个json,想要从中获取名称和数字并将其存储在数据库中



I have a below json in PHP and want to fetch name and number from it and store it in database

{
	"student": [{
		"Name": "John",
		"Number": "1234567890"
	}, {
		"Name": "Jonny",
		"Number": "0987654321"
	}, {
		"Name": "Tom",
		"Number": "0212365895"
	}]
}





我尝试过以下代码但无法获取代码:



I have tried below code but not able to fetch it:

<?php 
//http://localhost:8080/sample1/webservice2.php?json={%22UserName%22:1,%22FullName%22:2}
//$json=$_GET ['json'];
$json = file_get_contents('php://input');
$obj = json_decode($json);

echo $json;

//Save
$con=mysql_connect("localhost","USERNAME","PASSWORD");
mysql_select_db("DATASASE",$con);

  /* grab the posts from the db */
  //$query = "SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
  //for($i=0;i<count($obj);$i++){
	mysql_query("INSERT INTO Contact (Name, Number) VALUES ('".$obj->{'student'}{'Name'}."', '".$item->{'Number'}."')");
//}

mysql_close($con);
//
  //$posts = array($json);
  $posts = array(1);
    header('Content-type: application/json');
    echo json_encode(array('posts'=>$posts));

?>

推荐答案

json =


_GET ['json'];
_GET ['json'];


json = file_get_contents(' php:// input');
json = file_get_contents('php://input');


这篇关于从php中获取json的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆