我正在使用JSON文件在我的Android应用中工作。我正在关注一个教程,现在我已经检测到我正在使用的PHP JSON文件的输出格式有问题。
这是我的应用所需的输出格式:
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c203",
"name": "John Wayne",
"email": "john_wayne@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c204",
"name": "Angelina Jolie",
"email": "angelina_jolie@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c205",
"name": "Dido",
"email": "dido@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c206",
"name": "Adele",
"email": "adele@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c207",
"name": "Hugh Jackman",
"email": "hugh_jackman@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c208",
"name": "Will Smith",
"email": "will_smith@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c209",
"name": "Clint Eastwood",
"email": "clint_eastwood@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2010",
"name": "Barack Obama",
"email": "barack_obama@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2011",
"name": "Kate Winslet",
"email": "kate_winslet@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2012",
"name": "Eminem",
"email": "eminem@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
这是我用PHP文件获取的输出格式:
[{"idCategoria":"21","nombreCategoria":"Deporte"},{"idCategoria":"22","nombreCategoria":"Hosteler\u00eda y restauraci\u00f3n"},{"idCategoria":"23","nombreCategoria":"Moda y complementos"},{"idCategoria":"24","nombreCategoria":"Ocio y eventos"},{"idCategoria":"28","nombreCategoria":"Otros servicios"},{"idCategoria":"25","nombreCategoria":"Salud y belleza"},{"idCategoria":"26","nombreCategoria":"Servicios profesionales"},{"idCategoria":"27","nombreCategoria":"Tiendas y comercio"}]
请不要考虑内容和对象名称,我需要更改JSON格式:
这是我的PHP代码:
<?php
$host= 'localhost';
$db = 'xxxx';
$uid = 'xxx';
$pwd = 'xxx';
$link = mysql_connect($host,$uid,$pwd) or die("No se puede conectar ");
mysql_query("SET NAMES 'utf8'");
mysql_select_db($db) or die ("No se puede seleccionar la bbdd");
$arr = array();
$rs = mysql_query("SELECT * FROM tbcategorias ORDER BY nombreCategoria asc");
while ($obj = mysql_fetch_assoc($rs)){
$arr[] = $obj;
}
echo json_encode($arr);
?>
我应该在PHP代码中更改什么才能获得与我在上面的问题中向您展示的第一种情况相同的格式?
答案 0 :(得分:1)
json_encode有一个名为JSON_PRETTY_PRINT
的FLAG,因此您可以使用
echo json_encode($arr,JSON_PRETTY_PRINT);
如果你的意思是“格式”。
答案 1 :(得分:1)
我猜你的问题是php输出正在抛出一个对象数组,所以它有:
[{
"idCategoria":"21","nombreCategoria":"Deporte"
},{
"idCategoria":"22","nombreCategoria":"Hosteler\u00eda y restauraci\u00f3n"
}]
我猜你应该输出类似的内容:
{
"Categorias": [{
"idCategoria":"21","nombreCategoria":"Deporte"
}, {
"idCategoria":"22","nombreCategoria":"Hosteler\u00eda y restauraci\u00f3n"
}]
}
所以在“分类”中你有一个所有类别的数组,比如体育和酒店..;)
在PHP中我应该尝试:
$json = array("Categorias" => $arr);
echo json_encode($json);
我没有尝试过这个......但重要的是这个想法......
在这里你可以看到输出: