php 如何使用php生成json?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1435072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate json using php?
提问by Haluk
I've never used JSON before and I'm trying to utilize the following javascript: http://jqueryselectcombo.googlecode.com/files/jquery.selectCombo1.2.6.js
我以前从未使用过 JSON,我正在尝试使用以下 javascript:http: //jqueryselectcombo.googlecode.com/files/jquery.selectCombo1.2.6.js
It needs a JSON output in the following format:
它需要以下格式的 JSON 输出:
[{oV: 'myfirstvalue', oT: 'myfirsttext'},
{oV: 'mysecondvalue', oT: 'mysecondtext'}]
Could you guide me to an example on how to generate a JSON output as above, using PHP?
你能指导我如何使用 PHP 生成上述 JSON 输出的示例吗?
采纳答案by Amber
The simplest way would probably be to start with an associative array of the pairs you want:
最简单的方法可能是从你想要的对的关联数组开始:
$data = array("myfirstvalue" => "myfirsttext", "mysecondvalue" => "mysecondtext");
then use a foreach and some string concatenation:
然后使用 foreach 和一些字符串连接:
$jsontext = "[";
foreach($data as $key => $value) {
$jsontext .= "{oV: '".addslashes($key)."', oT: '".addslashes($value)."'},";
}
$jsontext = substr_replace($jsontext, '', -1); // to get rid of extra comma
$jsontext .= "]";
Or if you have a recent version of PHP, you can use the json encoding functions built in - just be careful what data you pass them to make it match the expected format.
或者,如果您有最新版本的 PHP,则可以使用内置的 json 编码函数 - 只需注意传递给它们的数据以使其与预期格式匹配。
回答by Pascal MARTIN
Once you have your PHP data, you can use the json_encodefunction ; it's bundled with PHP since PHP 5.2
获得 PHP 数据后,您就可以使用该json_encode函数;它自 PHP 5.2 起与 PHP 捆绑在一起
In your case you JSON string represents :
在您的情况下,您的 JSON 字符串表示:
- a list containing 2 elements
- each one being an object, containing 2 properties / values
- 包含 2 个元素的列表
- 每个都是一个对象,包含 2 个属性/值
In PHP, this would create the structure you are representing :
在 PHP 中,这将创建您所表示的结构:
$data = array(
(object)array(
'oV' => 'myfirstvalue',
'oT' => 'myfirsttext',
),
(object)array(
'oV' => 'mysecondvalue',
'oT' => 'mysecondtext',
),
);
var_dump($data);
The var_dumpgets you :
将var_dump让你:
array
0 =>
object(stdClass)[1]
public 'oV' => string 'myfirstvalue' (length=12)
public 'oT' => string 'myfirsttext' (length=11)
1 =>
object(stdClass)[2]
public 'oV' => string 'mysecondvalue' (length=13)
public 'oT' => string 'mysecondtext' (length=12)
And, encoding it to JSON :
并且,将其编码为 JSON :
$json = json_encode($data);
echo $json;
You get :
你得到 :
[{"oV":"myfirstvalue","oT":"myfirsttext"},{"oV":"mysecondvalue","oT":"mysecondtext"}]
BTW : Frolm what I remember, I'd say your JSON string is not valid-JSON data : there should be double-quotes arround the string, including the names of the objects properties
顺便说一句:从我记得的内容来看,我会说您的 JSON 字符串不是有效的 JSON 数据:字符串周围应该有双引号,包括对象属性的名称
See http://www.json.org/for the grammar.
有关语法,请参阅http://www.json.org/。
Hope this helps :-)
希望这可以帮助 :-)
回答by ennuikiller
This should be helpful: Generating JSON
这应该会有所帮助:生成 JSON
回答by MashukKhan
This is the php code to generate json format
这是生成json格式的php代码
<?php
$catId = $_GET['catId'];
$catId = $_POST['catId'];
$conn = mysqli_connect("localhost","root","","DBName");
if(!$conn)
{
trigger_error('Could not Connect' .mysqli_connect_error());
}
$sql = "SELECT * FROM TableName";
$result = mysqli_query($conn, $sql);
$array = array();
while($row=mysqli_fetch_assoc($result))
{
$array[] = $row;
}
echo'{"ProductsData":'.json_encode($array).'}'; //Here ProductsData is just a simple String u can write anything instead
mysqli_close('$conn');
?>
回答by sbaan da
You can use the stdClass, add the properties and json_encode the object.
您可以使用 stdClass,添加属性和 json_encode 对象。
$object = new stdClass();
$object->first_property = 1;
$object->second_property = 2;
echo '<pre>';var_dump( json_encode($object) , $object );die;
Voilà!
瞧!
string(40) "{"first_property":1,"second_property":2}"
object(stdClass)#43 (2) {
["first_property"]=>
int(1)
["second_property"]=>
int(2)
}

