我在php上发送json编码的对象;
在php上,我首先处理对象的简单字段(int、string、data),一切正常。
当我尝试获取数组字段时,我使用“}”而不是[1,2,…n](如果我在硬代码数据上尝试php,一切都正常)
我的类(java)1)
public class Sight {
public Sight() {
this.id = -1;
this.name = "";
this.photo = "";
this.info = "";
this.area_id = -1;
this.area = "";
this.update_date = new Date();
this.coordinates = "";
this.categoryId = -1;
this.category = "";
this.other_categories = new ArrayList<>();
this.tags_values = new ArrayList<>();
this.rate = -1;
}
@Expose
@SerializedName("object_id")
private int id;
@Expose
@SerializedName("name")
private String name;
@Expose
@SerializedName("photo")
private String photo;
@Expose
@SerializedName("info")
private String info;
@Expose
@SerializedName("area_id")
private int area_id;
@Expose
@SerializedName("area_name")
private String area;
@Expose
@SerializedName("update_date")
private Date update_date;
@Expose
@SerializedName("coordinates")
private String coordinates;
@Expose
@SerializedName("category_id")
private int categoryId;
@Expose
@SerializedName("category_name")
private String category;
@Expose
@SerializedName("other_categories")
public List<Integer> other_categories;
@Expose
@SerializedName("tags_values")
public List<Integer> tags_values;
@Expose
@SerializedName("success")
private Boolean success;
@Expose
@SerializedName("message")
private String message;
@Expose
@SerializedName("rate")
private int rate;
}
php代码:
<?php
require_once "conn.php";
$result = array();
$content = trim(file_get_contents("php://input"));
$input = json_decode($content , true);
//this lines only for debug!!! --->
//to get json code of php-object
array_push($result, json_encode($input));
// to get array
$temp = $content['other_categories'];
array_push($result, json_encode($temp));
//to get decoded array
$temp = json_decode($content['other_categories'] , true);
array_push($result, json_encode($temp));
//these lines only for debag!! ---<
$newname = $connection->real_escape_string($input['name']);
$description = $connection->real_escape_string($input['info']);
$area_id = $connection->real_escape_string($input['area_id']);
$category_id = $connection->real_escape_string($input['category_id']);
$tags = $connection->real_escape_string($input['tags_values']);
$other_categories= $connection->real_escape_string($input['other_categories']);
$photo = $connection->real_escape_string($input['photo']);
$coordinates = $connection->real_escape_string($input['coordinates']);
$query_insert_object = "INSERT INTO `object`(`name`, `photo`, `info`, `area_id`, `update_date`, `coordinates`)
VALUES ('$newname', '$photo', '$description', $area_id, NOW(), '$coordinates')";
$query_id = "SELECT object_id FROM object ORDER BY object_id DESC LIMIT 1";
if ($res = mysqli_query($connection, $query_insert_object)){
$temp = mysqli_query($connection, $query_id);
$id = mysqli_fetch_assoc($temp)['object_id'];
$query_insert_main_category = "INSERT INTO `category_of_object`(`object_id`, `category_id`, `main`)
VALUES ('$id','$category_id',1)";
if ($res1 = mysqli_query($connection, $query_insert_main_category)){
//code exactly go here, but cicles for and foreach never run ( $other_categories = null and $tags = null )
for ($i = 1; $i < count($other_categories); $i++){
$value=$other_categories[$i];
array_push($result, $value);
$query_insert_other_categories = "INSERT INTO `category_of_object`(`object_id`, `category_id`, `main`)
VALUES ($id, $value ,0)";
if(!($res2 = mysqli_query($connection, $query_insert_other_categories)))
{
array_push($result, mysqli_error($connection));
echo "categories";
};
}
foreach ($tags as $value) {
array_push($result, $value);
$query_insert_tags = "INSERT INTO `various_of_objects`(`various_of_characteristic_id`, `object_id`)
VALUES ('$value','$id')";
if (!($res3 = mysqli_query($connection, $query_insert_tags)))
{
array_push($result, mysqli_error($connection));
//echo "tags";
};
}
// Any of there line below I don't have in result! And all queries work fine on hard code data.
} else{
array_push($result, mysqli_error($connection));
//echo "category";
}
} else {
array_push($result, mysqli_error($connection));
// echo "object";
}
echo json_encode($result);
mysqli_close($connection);
?>
3/你怎么看,我在结果里放了一些数据进行调试。这是我的结果(来自php),result.size()=3:php对象:(array_push($result,json_encode($input));)
result[0] =
"{"area_name":"",
"area_id":12,
"category_name":"",
"category_id":1,
"coordinates":"50, 60",
"object_id":-1,"info":"",
"name":"ewdfew",
"other_categories":[2,3,4,5],
"photo":"ewdfew 2511.7730756090899.jpg",
"rate":-1,
"tags_values":[1],
"update_date":"Dec 5, 2020 11:46:49 PM"}"
如您所见,有几个字段是空的,但我不使用它们!我需要数组,其他的类别和标签
数组(
array_push($result, json_encode($temp));
$temp = json_decode($content['other_categories'] , true);
array_push($result, json_encode($temp));:
和结果
result[1] = ""{""
result[2] = "null"
我用“{”代替数组。
非常感谢您的建议!我真的需要帮助。
1条答案
按热度按时间v7pvogib1#
非常感谢大家的评论!所以,我只需要移除
对于数组字段:
现在起作用了