0

我相信格式正确的 json 字符串有问题。我想知道这是否是由于查询字符串的构造造成的,但它看起来对我来说是正确的,除非下面编码的 json 字符串中的双括号有问题。寻找一双额外的眼睛。

在 json 编码之前, var_dump($considerationCodes) 看起来像:

array (size=2)
  8 => 
    array (size=13)
      67 => 
        array (size=1)
          0 => 
            array (size=3)
              ...
      41 => 
        array (size=1)
          0 => 
            array (size=3)
              ...
      42 => 
        array (size=1)
          0 => 
            array (size=3)
              ...

打印 json_encode($considerationCodes); 好像:

$considerationCodes = 
{"8": {"67":  [[  {"id":"64","description":"string description..."},{"id":"65","description":"string description..., "},{"id":"66","description":"string description..."}  ]]  , "41":  [[  {"id":"64","description":"string description..."},{"id":"65","description":"string description..., "},{"id":"66","description":"string description..."}  ]] }}  

Json 构造它是如何被预期/接受的(测试并使用原始数据):

['8']['67']['64'] = "string description...";
['8']['67']['65'] = "string description...";
['8']['67']['66'] = "string description...";
['8']['41']['64'] = "string description...";
['8']['41’]['65'] = "string description...";
['8']['41']['66'] = "string description...";
...etc

这就是我提交 json 编码的方式(经过测试并使用原始数据):

return $this->renderText(json_encode( $considerationCodes[$appCode][$reasonCode] ) ); 

当我查看这个 json 提交的结果(多选选择输入)时,它看起来像:

[Object][object][Object][object][Object][object][Object][object]

我错过了什么?

编辑:

这就是我构建数组的方式:

$reason_codes_ids = array();
        foreach($all_reason_codes as $key => $values) {
            $reason_codes_ids[] = $values['id'];
        }
        //wrap the consideration_info into each reason_id:        
        $codes_with_reasons = array();
        foreach($reason_codes_ids as $value){
            foreach($consideration_info as $key => $value1){
                $codes_with_reasons[$value][$key] = $value1;
            }
        }
        //the above results in: ['reason-code-id']['consideration-code-id'] = "consideration-code-answer":
        //next, get the declined_app_status:
        $declinedAppStatus = 8;
        //then wrap the above results in a wraper for the declined application-code
        $declinedConsiderationCodes = array();
        foreach($codes_with_reasons as $key => $value)
        {
            $declinedConsiderationCodes[$declinedAppStatus][$key]=[$value];
        }
        $considerationCodes = $declinedConsiderationCodes;

和:

$consideration_info =

array (size=3)
  0 => 
    array (size=2)
      'id' => string '64' (length=2)
      'description' => string 'string...' (length=35)
  1 => 
    array (size=2)
      'id' => string '65' (length=2)
      'description' => string ''string...' (length=143)
  2 => 
    array (size=2)
      'id' => string '66' (length=2)
      'description' => string ''string...' (length=143)

$reason_codes_ids =

array (size=13)
  0 => string '67' (length=2)
  1 => string '41' (length=2)
  2 => string '42' (length=2)
  3 => string '43' (length=2)
  4 => string '44' (length=2)
  5 => string '45' (length=2)
  6 => string '46' (length=2)
  7 => string '47' (length=2)
  8 => string '48' (length=2)
  9 => string '49' (length=2)
  10 => string '50' (length=2)
  11 => string '51' (length=2)
  12 => string '68' (length=2)
4

1 回答 1

0

使用正确的格式和缩进,您的 JSON 字符串如下所示:

{
    "8": {
        "67": [
            [
                {
                    "id":"64",
                    "description":"string description..."
                },
                {
                    "id":"65",
                    "description":"string description..., "
                },
                {
                    "id":"66",
                    "description":"string description..."
                }
            ]
        ],
        "41": [
            [
                {
                    "id":"64",
                    "description":"string description..."
                },
                {
                    "id":"65",
                    "description":"string description..., "
                },
                {
                    "id":"66",
                    "description":"string description..."
                }
            ]
        ]
    }
}

请注意,这['8']['67']是一个带有数字索引的数组。要执行您想要执行的操作,您的 JSON 字符串应该看起来更像这样:(示例

{
    "8": {
        "67": {
            "64":"string description...",
            "65":"string description..., ",
            "66":"string description..."
        },
        "41": {
            "64":"string description...",
            "65":"string description..., ",
            "66":"string description..."
        }
    }
}

所以你的 PHP 数组应该看起来更像这样:

$considerationCodes = array(
    "8" => array(
        "67" => array(
            "64" => "string description...",
            "65" => "string description..., ",
            "66" => "string description..."
        ),
        "41" => array(
            "64" => "string description...",
            "65" => "string description..., ",
            "66" => "string description..."
        )
    )
);
于 2012-09-20T20:17:56.977 回答