如何用php编码json

时间:2017-09-30 07:11:56

标签: php json

如何使用php

在json下编码
'{ "chart": { "caption": "Actual Revenues, Targeted Revenues & Profits", "subcaption": "Last year", "xaxisname": "Month", "yaxisname": "Amount (In USD)", "numberprefix": "$", "theme": "zune" },

  "categories": [ { "category": [ { "label": "Jan" }, { "label": "Feb" }, { "label": "Mar" }, { "label": "Apr" }, { "label": "May" }, { "label": "Jun" }, { "label": "Jul" }, { "label": "Aug" }, { "label": "Sep" }, { "label": "Oct" }, { "label": "Nov" }, { "label": "Dec" } ] } ], 

"dataset": [ { "seriesname": "Actual Revenue", "data": [ { "value": "16000" }, { "value": "20000" }, { "value": "18000" }, { "value": "19000" }, { "value": "15000" }, { "value": "21000" }, { "value": "16000" }, { "value": "20000" }, { "value": "17000" }, { "value": "25000" }, { "value": "19000" }, { "value": "23000" } ] }, 
  { "seriesname": "Projected Revenue", "renderas": "line", "showvalues": "0", "data": [ { "value": "15000" }, { "value": "16000" }, { "value": "17000" }, { "value": "18000" }, { "value": "19000" }, { "value": "19000" }, { "value": "19000" }, { "value": "19000" }, { "value": "20000" }, { "value": "21000" }, { "value": "22000" }, { "value": "23000" } ] }, 
  { "seriesname": "Profit", "renderas": "area", "showvalues": "0", "data": [ { "value": "4000" }, { "value": "5000" }, { "value": "3000" }, { "value": "4000" }, { "value": "1000" }, { "value": "7000" }, { "value": "1000" }, { "value": "4000" }, { "value": "1000" }, { "value": "8000" }, { "value": "2000" }, { "value": "7000" } ] } ] }');

4 个答案:

答案 0 :(得分:0)

使用- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } 从JSON创建PHP对象。

答案 1 :(得分:0)

答案 2 :(得分:0)

其变化简单var_dump(json_decode($json, true));

DEMO

答案 3 :(得分:0)

<?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";

$myJSON = json_encode($myObj);

echo $myJSON;
?>

output
{"name":"John","age":30,"city":"New York"}

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

output
object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}