Tuesday, 15 May 2012

how do you pick certain fields in json format in php -


I am calling a URL that gives the data as Jason format. I like to select some variables from this output. I type ouput: <"> graph_property": [{"name": "calculation_method", "value": "geo mean"}, {{"Id": "value": "time"); "measure": [{"id": "1132282", "alias": "example.com" "bucket_data": [{" Name ":" "" 1 "," id ": 1," perf_data ": {" value ":" 4.878 "," unit ":" seconds "}," avail_data ": { {"Value": "100.00", "unit": "percent"}, "data_ count": {"value": "1", "entity": "#"}}, {"name": "2013-June - "" "," Id ": 2," data ": {" value ":" - "," unit ":" seconds "}," avail_data ": {" value ":" - "," entity ":" Percentages "}," data_ count ": {" value ":" - "," unit ":" # "}}, {" name ":" 2013-June-20 11:33 flood {"Value": "-", "unit": "percent", "perf_data": {"value": "-", "unit": "seconds"}, "avail_data": {"value": "percent" "[" "Name": "perfwarning", "value": "-", "unit"}, "data_ count": {"value": "-", "unit" "" "}", "graph_pace" "": "Value": "value": "value": "-" "value": "value": " "Value": "value": "value": "value": "value": "value": "value": "value": "value": "value": "value": "value": "value": "value": "value" "" ":" "": "" "", "Unit"}, "unit": {"name": "lines", "value": "3", "unit": "" "," name ":" page "," Value ":" user time "," unit ":" seconds "}, {"Name": "avg_perf", "value": "4.878", "unit": "seconds"}, {"name": "avg_avail", "value": "100.00", "unit": "percentage" } "Link": {"type": "app / jason", "{"} "," value ":" total_datapoint_count "," value ":" 1 "," unit ":" # "}, {}]}]," link " Href ":" http://api.keynote.com/ "," Rel ":" slotmetadata "}}

The interest I am interested in: < "", "Value": "4.878", "name": "2013-Jun-20 11:28 am", "price": " - "," name ":" 2013-Jun-20 11:33 AM "," value ":" - ",

The reason for this is that sometimes, web service calls Return Commodity Value With this output, I am the latest Choose the position in which there is no empty value and then print the name and value which is not empty.

Is there an easy way to choose the name and pricing field from this JSN output?

When I

  $ data & lt; -json_decode ($ resp) print_r ($ data)   

One part of the data is this:

  [measure] => Array ([0] = & gt; Array ([ID] => 1132282 [nickname] => Example [Beckett_data] => Hey ([0] = & gt; Hey ([name] = & Gt; [2013] - [20] 01:23 PM [ID] => 1 [perf_data] => Hey ([value] => 3.074 [unit] => 2] [avail_data] = ([Value] => 100.00 [unit] = & gt; percent) [data_ count] = & gt; array ([value] => 1 [unit] => gt; #)) [ 1] = & gt; Hey ([name] = & gt; 2013-Jun [20] = 1] 2 [perf_data] => array ([value] => 3.416 [unit] => seconds ) [Avail_data] = & gt; array ([value] => 100.00 [unit] => gt; [data_ count] = & gt; array ([value] => 1 [unit] = & gt; ; #)) [2] = & gt; Hey ([name] = & gt; 2013-Jun-20 01:33 pm [ID] => 3 [perf_data] => array ([value] => - [unit] = & gt; Seconds] [avail_data] = & gt; Hey ([value] = & gt; [unit] = & gt; percent) [data_ count] = & gt; array ([value] = & gt; - [unit] = & gt;  

Use the built-in function

  $ data = json_decode ('your_json_here'); // $ data will be "stdClass" object $ data = json_decode ('your_json_here', true); // $ data will be "array" object   

Then you can work with $ array with regular array / object

if type = stdClass:

  $ data-> Measurement [0] - & gt; Bucket_data [0] - & gt; Name; // will be equal "2013-Jun-20 11:23 AM" if type = array  
  $ data ['measure'] [0] ['Bucket_data'] [0] ['name']; // will be equal "2013-Jun-20 11:23 AM"    

No comments:

Post a Comment