简体   繁体   English

如何在php中将json转换为字符串

[英]How to convert json to string in php

I want to convert json data to String in php. 我想将json数据转换为php中的String。 This is my json data 这是我的json数据

    {
        "": [{
            "description": "hello, this is description speaking"
        }, {
            "fieldName": "myfile",
            "originalFilename": "image.png",
            "path": "/var/folders/rq/q_m4_21j3lqf1lw48fqttx_80000gn/T/ttzVoNPfBxMMirec1tJsnrd2.png",
            "headers": "[Object]",
            "size": "82745"
        }]
    }

I want to print "hello, this is description speaking" in my php file and also i want to upload my file. 我想在我的php文件中打印“你好,这是描述性发言”,我也想上传我的文件。 My php code is given below please correct it. 我的php代码在下面给出,请更正。

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
 $json = file_get_contents("php://input");
    $json_obj = json_decode($json);

    echo $json_obj;

    echo $_POST['description'];

    $name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];
$error = $_FILES['myfile']['error'];

if (!empty($name)) {
    $location = 'uploads/';

    if  (move_uploaded_file($tmp_name, $location.$name)){
        echo 'Uploaded';
    }

} else {
    echo 'please choose a file';
}
}  else {
    echo "error 2";
}

In this code errors are Undefined index: file in D:\\xampp\\htdocs\\work. 在此代码中,错误是未定义的索引:D:\\ xampp \\ htdocs \\ work中的文件。 And without the fileupload code it shows nothing. 如果没有fileupload代码,它什么也不会显示。

If you just want to access the description property of your json, you can do the following: 如果您只想访问json的description属性,则可以执行以下操作:

$json_obj = json_decode($json, true);
$description = $json_obj[''][0]['description'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM