用php编码为JSON格式

时间:2014-03-28 18:18:30

标签: php json

应该是小菜一碟,但我不知道如何做到这一点,并且一直试图弄清楚它。

我写了以下.php:

<?php 
$data = array( "query" =>  array("code" => "ContentsCode", 
"selection" => array("filter" => "item", "values" => array
("BE0101N1"))), "code" => "Tid", "selection" => array("filter" => 
"item", "values" => array("2010", "2011" )), "response" => array
("format" =>"json")); 

$data_string = json_encode($data);
 echo var_dump($data_string);
?>

外观如何:

{
    "query": {
        "code": "ContentsCode",
        "selection": {
            "filter": "item",
            "values": [
                "BE0101N1"
            ]
        }
    },
    "code": "Tid",
    "selection": {
        "filter": "item",
        "values": [
            "2010",
            "2011"
        ]
    },
    "response": {
        "format": "json"
    }
}

它应该如何看待:

{
    "query": [
        {
            "code": "ContentsCode",
            "selection": {
                "filter": "item",
                "values": [
                    "BE0101N1"
                ]
            }
        },
        "code": "Tid",
        "selection": {
            "filter": "item",
            "values": [
                "2010",
                "2011"
            ]
        }
    ],
    "response": {
        "format": "json"
    }
}

唯一的区别是查询后的“[”和第2行末尾的“]”。

如何获得括号?我的代码中缺少什么?

谢谢!

3 个答案:

答案 0 :(得分:1)

正确放置它时,更容易理解问题。你有什么:

$data = array(
    "query" => array(
        "code" => "ContentsCode", 
        "selection" => array(
            "filter" => "item",
            "values" => array(
                "BE0101N1"
            )
        )
    ),
    "code" => "Tid",
    "selection" => array(
        "filter" =>  "item",
        "values" => array(
            "2010",
            "2011"
        )
    ),
    "response" => array(
        "format" => "json"
    )
);

这是你想要的(评论以帮助可视化它如何转换为JSON):

$data = array(
#   query: [
    "query" => array(
#       {
        array(
            "code" => "ContentsCode",
            "selection" => array(
                "filter" => "item",
                "values" => array(
                    "BE0101N1"
                )
            )
#       },
        ),
#       {
        array(
            "code" => "Tid",
            "selection" => array(
                "filter" => "item",
                "values" => array(
                    "2010",
                    "2011"
                )
            )
#       }
        )
#   ],
    ),
#   response: {
    "response" => array(
        "format" => "json"
#   }
    )
);

答案 1 :(得分:1)

如果你缩进你的代码,那将是显而易见的

$data = array( 
    "query" => array(
        "code" => "ContentsCode", 
        "selection" => array(
            "filter" => "item", 
             "values" => array("BE0101N1")
         )
     ), 
     "code" => "Tid",  
     "selection" => array(
         "filter" => "item", 
         "values" => array("2010", "2011" )
     ),
     "response" => array(
         "format" => "json"
     )
);

您需要一个包含“code”

的数组周围的数组
$data = array( 
    "query" => array(
        array(        
            "code" => "ContentsCode", 
            "selection" => array(
                "filter" => "item", 
                "values" => array("BE0101N1")
            )
        ),
        array(
            "code" => "Tid",  
            "selection" => array(
                "filter" => "item", 
                "values" => array("2010", "2011" )
            ),        
        )
     ), 
     "response" => array(
          "format" => "json"
     )
);

答案 2 :(得分:0)

$data = array(   
    "query" =>  array(
      array(
        "code" => "ContentsCode", 
        "selection" => array(
          "filter" => "item", 
          "values" => array("BE0101N1")
        )
      ), 
      "code" => "Tid", 
      "selection" => array(
        "filter" => "item", 
        "values" => array("2010", "2011" )
      ) 
    ),

  "response" => array("format" =>"json")
); 

$json_data = json_encode($data);