parse Javascript json into php -
here json. ia dwr response ( webservice created in java) .
{ key1 : { date : "today" , items : [ { itemkey1 : "itemvalue1", itemkey2 : "itemvalue2", }, { itemkey1 : "itemvalue1", itemkey2 : "itemvalue2", }, ] } }
json lint showing error.
if can see key not have "" may that's why can not parse json in php directly. there way oi can parse json , array or directly array.
but when transfor this type of json it. in json lint shows proper json.
{ "key1": { "date": "today", "items": [ { "itemkey1": "itemvalue1", "itemkey2": "itemvalue2" }, { "itemkey1": "itemvalue1", "itemkey2": "itemvalue2" } ] } }
so there anyway can trasnfer json second type. dynamically in php
since there no javascript parser build in php , have here javascript , not json, options implement own parser / use existing parser. or wrangle string being json, done regex, though flaky.
for specified example data, do:
<?php $data = json_decode(preg_replace_callback('#(\s+)\s*:#i', function($matches){ return '"'.$matches[1].'" :'; },$str));
Comments
Post a Comment