在php中将字符串转换为json

在php中将字符串转换为json,php,arrays,string,json,Php,Arrays,String,Json,是否可以将这些字符串(从json格式转换为字符串)读入php数组 jQuery: $.ajax({ type: 'GET', url: 'index.php?task=search&wtd=ul', data: $('#fupdate').serialize()+'&derulo='+$('#getdbresult').val(), beforeSend: function(){

是否可以将这些字符串(从json格式转换为字符串)读入php数组

jQuery:

  $.ajax({

            type: 'GET',
            url: 'index.php?task=search&wtd=ul',
            data: $('#fupdate').serialize()+'&derulo='+$('#getdbresult').val(),
            beforeSend: function(){
                $('.the-modal').html('Updating...');
            }, 
            success: function(d){
                $('.the-modal').html(d);
                console.log(d);
            }

    });
PHP:

var_dump(json_解码(“{$_请求['derulo']}”,true))

价值观:

string(379)"[{\"target_detail_id\":\"66351031\",\"first_name\":\"Dorothy\",\"last_name\":\"Smith\",\"company\":\"Active Money Now\",\"sic_code\":\"\",\"position\":\"\",\"target\":\"1300572544\",\"website\":\"\",\"email\":\"dorothy@activemoney.com.au\",\"email_status\":\"\",\"country\":\"Australia\",\"city\":\"Broken Hill\",\"postal_code\":\"2880\",\"address\":\"Po Box 41\",\"note\":\"\"}]" 
结果:

NULL

我尝试使用
json\u decode
,但什么也没出现。 还研究了如何使用
内爆
相应地分离字符串。 但是,还有别的办法吗

编辑:

它可以使用:
$object=(json_解码(stripslashes($_请求['derulo']))

您需要使用(“解码JSON字符串”)而不是(“返回值的JSON表示形式”)

假设您的输入是正确的(您给出的示例是正确的),返回值将是一个表示JSON数据的对象。您还可以将结果作为数组获取(参数参考请参见文档)


编辑:您的代码肯定还有其他问题,请发布完整的代码。
(我的猜测是,您没有检查返回值,而是希望json_decode通过引用修改参数,只检查参数变量——这就解释了为什么“什么都没有发生”)

以下工作:

()

PHP代码:

<?php

$json = <<<JSON
[{"target_detail_id":"66351031","first_name":"Dorothy","last_name":"Smith","company":"Active Money Now","sic_code":"","position":"","target":"1300572544","website":"","email":"dorothy@activemoney.com.au","email_status":"","country":"Australia","city":"Broken Hill","postal_code":"2880","address":"Po Box 41","note":""}]
JSON;

$object = json_decode($json);
$array = json_decode($json, true);

var_dump($object);
var_dump($array);
array(1) {
  [0]=>
  object(stdClass)#1 (15) {
    ["target_detail_id"]=>
    string(8) "66351031"
    ["first_name"]=>
    string(7) "Dorothy"
    ["last_name"]=>
    string(5) "Smith"
    ["company"]=>
    string(16) "Active Money Now"
    ["sic_code"]=>
    string(0) ""
    ["position"]=>
    string(0) ""
    ["target"]=>
    string(10) "1300572544"
    ["website"]=>
    string(0) ""
    ["email"]=>
    string(26) "dorothy@activemoney.com.au"
    ["email_status"]=>
    string(0) ""
    ["country"]=>
    string(9) "Australia"
    ["city"]=>
    string(11) "Broken Hill"
    ["postal_code"]=>
    string(4) "2880"
    ["address"]=>
    string(9) "Po Box 41"
    ["note"]=>
    string(0) ""
  }
}
array(1) {
  [0]=>
  array(15) {
    ["target_detail_id"]=>
    string(8) "66351031"
    ["first_name"]=>
    string(7) "Dorothy"
    ["last_name"]=>
    string(5) "Smith"
    ["company"]=>
    string(16) "Active Money Now"
    ["sic_code"]=>
    string(0) ""
    ["position"]=>
    string(0) ""
    ["target"]=>
    string(10) "1300572544"
    ["website"]=>
    string(0) ""
    ["email"]=>
    string(26) "dorothy@activemoney.com.au"
    ["email_status"]=>
    string(0) ""
    ["country"]=>
    string(9) "Australia"
    ["city"]=>
    string(11) "Broken Hill"
    ["postal_code"]=>
    string(4) "2880"
    ["address"]=>
    string(9) "Po Box 41"
    ["note"]=>
    string(0) ""
  }
}

编辑2:

  • json\u decode
    参数周围不需要引号,只需使用

    json_decode($_REQUEST['derulo'], true);
    
    但这并不是导致问题的原因(这只是效率低下,php必须解析字符串中的另一个变量)

  • 您的php代码段工作正常,因此您一定从查询中获取了错误的数据。你可以很容易地用

    var_dump($_REQUEST['derulo']);
    
    当使用
    GET
    请求或切换到POST时,您不应该在url和
    data
    上混合发送数据。我建议让jQuery负责数据的序列化,例如

    $.ajax({
        type: 'GET',
        url: 'index.php',
        data: {
          'task':'search',
          'wtd':'ul',
          'derulo':JSON.stringify($('#getdbresult').val())
        },
        beforeSend: function(){
            $('.the-modal').html('Updating...');
        }, 
        success: function(d){
            $('.the-modal').html(d);
            console.log(d);
        }
      });
    
  • 您需要使用(“解码JSON字符串”)而不是(“返回值的JSON表示形式”)

    假设您的输入是正确的(您给出的示例是正确的),返回值将是一个表示JSON数据的对象。您还可以将结果作为数组获取(参数参考请参见文档)


    编辑:您的代码肯定还有其他问题,请发布完整的代码。
    (我的猜测是,您没有检查返回值,而是希望json_decode通过引用修改参数,只检查参数变量——这就解释了为什么“什么都没有发生”)

    以下工作:

    ()

    PHP代码:

    <?php
    
    $json = <<<JSON
    [{"target_detail_id":"66351031","first_name":"Dorothy","last_name":"Smith","company":"Active Money Now","sic_code":"","position":"","target":"1300572544","website":"","email":"dorothy@activemoney.com.au","email_status":"","country":"Australia","city":"Broken Hill","postal_code":"2880","address":"Po Box 41","note":""}]
    JSON;
    
    $object = json_decode($json);
    $array = json_decode($json, true);
    
    var_dump($object);
    var_dump($array);
    
    array(1) {
      [0]=>
      object(stdClass)#1 (15) {
        ["target_detail_id"]=>
        string(8) "66351031"
        ["first_name"]=>
        string(7) "Dorothy"
        ["last_name"]=>
        string(5) "Smith"
        ["company"]=>
        string(16) "Active Money Now"
        ["sic_code"]=>
        string(0) ""
        ["position"]=>
        string(0) ""
        ["target"]=>
        string(10) "1300572544"
        ["website"]=>
        string(0) ""
        ["email"]=>
        string(26) "dorothy@activemoney.com.au"
        ["email_status"]=>
        string(0) ""
        ["country"]=>
        string(9) "Australia"
        ["city"]=>
        string(11) "Broken Hill"
        ["postal_code"]=>
        string(4) "2880"
        ["address"]=>
        string(9) "Po Box 41"
        ["note"]=>
        string(0) ""
      }
    }
    array(1) {
      [0]=>
      array(15) {
        ["target_detail_id"]=>
        string(8) "66351031"
        ["first_name"]=>
        string(7) "Dorothy"
        ["last_name"]=>
        string(5) "Smith"
        ["company"]=>
        string(16) "Active Money Now"
        ["sic_code"]=>
        string(0) ""
        ["position"]=>
        string(0) ""
        ["target"]=>
        string(10) "1300572544"
        ["website"]=>
        string(0) ""
        ["email"]=>
        string(26) "dorothy@activemoney.com.au"
        ["email_status"]=>
        string(0) ""
        ["country"]=>
        string(9) "Australia"
        ["city"]=>
        string(11) "Broken Hill"
        ["postal_code"]=>
        string(4) "2880"
        ["address"]=>
        string(9) "Po Box 41"
        ["note"]=>
        string(0) ""
      }
    }
    

    编辑2:

  • json\u decode
    参数周围不需要引号,只需使用

    json_decode($_REQUEST['derulo'], true);
    
    但这并不是导致问题的原因(这只是效率低下,php必须解析字符串中的另一个变量)

  • 您的php代码段工作正常,因此您一定从查询中获取了错误的数据。你可以很容易地用

    var_dump($_REQUEST['derulo']);
    
    当使用
    GET
    请求或切换到POST时,您不应该在url和
    data
    上混合发送数据。我建议让jQuery负责数据的序列化,例如

    $.ajax({
        type: 'GET',
        url: 'index.php',
        data: {
          'task':'search',
          'wtd':'ul',
          'derulo':JSON.stringify($('#getdbresult').val())
        },
        beforeSend: function(){
            $('.the-modal').html('Updating...');
        }, 
        success: function(d){
            $('.the-modal').html(d);
            console.log(d);
        }
      });
    

  • 附加到问题的代码已经用json格式化。因此,您需要使用json_解码函数对其进行解码。它返回一个对象,只需将其类型转换为数组。 请注意,json代码本身就是一个数组(所有内容都在方括号内),因此我们必须参考第一项($item[0])


    Json_decode还提供了一个自动返回数组的选项:Json_decode($Json,true)。

    您附加到问题的代码已经用Json格式化。因此,您需要使用json_解码函数对其进行解码。它返回一个对象,只需将其类型转换为数组。 请注意,json代码本身就是一个数组(所有内容都在方括号内),因此我们必须参考第一项($item[0])


    Json_decode还提供了一个自动返回数组的选项:Json_decode($Json,true)。

    也许你应该使用Json_decode?…我已经重新表述了这个问题,我确实使用了Json_decode。打字错误如果是,什么意思是“没有出现”?您是否尝试了json解码(“…json…”,true)?是的。也试过了,但是当打印时什么也没有出现。发布你的实际函数调用以及你是如何检查结果的。这个例子是有效的json,没有理由不起作用。也许你应该使用json_decode?…我已经重新表述了这个问题,我确实使用了json_decode。打字错误如果是,什么意思是“没有出现”?您是否尝试了json解码(“…json…”,true)?是的。也试过了,但是当打印时什么也没有出现。发布你的实际函数调用以及你是如何检查结果的。这个例子是有效的json,没有理由不起作用。我已经重新表述了这个问题,我确实使用了json_decode。我认为
    $\u请求
    值没有用字符串括起来。这就是为什么json_decode无法读取值的原因,因为StringTested尝试了您建议的jquery,结果现在有
    string(379)
    在开始时使用
    var_dump
    将编辑问题的结果我重新表述了问题,我确实使用了json_decode。我认为
    $\u请求
    值没有作为字符串括起来。这就是为什么json_decode无法读取您建议的jquery的值,结果现在是
    string(379)
    开始时使用
    var_dump
    将编辑问题的结果