How do I go about parsing the json request below in PHP to extract specific values (in value ) like the following: studentName, studentemail and courseName? There will be only one entry per request and I will not need the rest of the values. I need to store these as PHP variables to be written to MYSQL. The parsing is where I am having trouble
See json below
{"count":7,"data":[{"name":"Timestamp","ordinal":0,"value":"3/5/2013 2:45:31 PM"},{"name":"studentName","ordinal":1,"value":"mikejones"},{"name":"studentEmail","ordinal":2,"value":"mikejones%40gmail.com"},{"name":"courseName","ordinal":3,"value":"Grade%20Test"},{"name":"cpQuizInfoPassFail","ordinal":4,"value":"Fail"},{"name":"cpInfoPercentage","ordinal":5,"value":"75"},{"name":"institutionCode","ordinal":6,"value":"1212"}]}{"count":0}{"subject":"Course Transcript","footer":"Thank you for your participation!","footerURL":"https://sites.google.com/site/test393223837/googleformsdemotranscript/footerLogo.jpg","correctURL":"http//abc.com,"enabled":true,"correctResponseText":"Correct Response","showCorrectAnswers":true,"description":"Please keep this for your records as proof of course completion and certification.","incorrectURL":"https://sites.google.com/site/test3232393837/_/rsrc/1342229575450/googleformsdemotranscript/redX.png","fromName":"The Training Team","replyTo":"","headerURL":"https://sites.google.com/site/test3938232337/googleformsdemotranscript/CaptivateDev.jpg","isHTML":true,"title":"Course Transcript","to":"joe#gmail.com","userResponseText":"Your Response"}
You can use json_decode function, that parse json code to php objects and arrays. See documentation for more info.
One problem is your json is invalid. Check with:
http://jsonlint.com/ or
http://jsonformatter.curiousconcept.com/
Related
I've seen the answers to similar questions - but they do not solve the issue I'm having. So here goes..
I have a response from a MailChimp EXPORT API that looks like this:
["Email Address","First Name","Last Name","Source","Open","MEMBER_RATING","OPTIN_TIME","OPTIN_IP","CONFIRM_TIME","CONFIRM_IP","LATITUDE","LONGITUDE","GMTOFF","DSTOFF","TIMEZONE","CC","REGION","LAST_CHANGED","LEID","EUID","NOTES"]
["email1#somewhere.com","","","BORN","",2,"",null,"2015-05-07 15:25:48",null,null,null,null,null,null,null,null,"2015-05-07 15:25:48","74554133","ar5jiewpkc",null]
["email2#somwhere.com","","","BORN","",2,"",null,"2015-05-07 15:25:48",null,null,null,null,null,null,null,null,"2015-05-07 15:25:48","74554137","ar5jiewpkc",null]
What I need to do is two things:
Remove the first stream object (containing "Email Address" .. etc)
Combine the rest of the objects into a single JSON response to pass to a script as if it were a single regular JSON response.
This is a JSON "Stream" that I need to turn into a regular JSON response.
I've successfully turned them all into arrays, but when I try to json_encode the array of arrays, I get back escaped data that is not a true JSON object.
Any help is appreciated!
I have a json_string in my database.
I echo and parse it to an object in javascript
I do
$.parseJSON('<?php echo $json_string;?>');
I get a json parse error.
What should I be doing?
This is my json_String
{"patches":[[{"diffs":[[1,"\u000a\u000a printhellon() {\u000a\u000a\u000a}d\u000a\u000a\u000a"]],"start1":0,"start2":0,"length1":0,"length2":26}],[{"diffs":[[0,") {\u000a\u000a\u000a}d"],[1,"s"],[0,"\u000a\u000a\u000a"]],"start1":15,"start2":15,"length1":11,"length2":12}],[{"diffs":[[0," {\u000a\u000a\u000a}ds"],[1,"d"],[0,"\u000a\u000a\u000a"]],"start1":16,"start2":16,"length1":11,"length2":12}]],"times":[1314489779299,1314489779408,1314489779581]}
I think JSON parsers don't like line breaks in strings for some reason. Parsing worked for me after removing the \u000a characters.
Edit: just like Brad said, it would be better to include the code directly as an object. Parsing JSON is usually more useful for data obtained using Ajax or something.
From your example, it appears like you're trying to insert PHP code into your javascript. You can't use PHP like that. PHP is server side, while Javascript runs in the browser after the page has been downloaded.
If you must get data from PHP to your javascript, you need to use AJAX. It's actually really easy with JQuery. Check out http://api.jquery.com/jQuery.ajax/
Trying to figure out a way to get ActionScript to print out a php object. In PHP, I can just do print_r, and I'm able to successfully get the object into Flash, but now can't figure out how to print out the object using ActionScript 3?
Well, when the request first comes over, there is always URLLoader.data, which is data in its raw form. If you're looking to get the data from a JSON-style object, there is for( var i:String in obj ). If you're looking for things more obscure than that, there is the ability to use describeType. My favorite example of the last one is in the JSONEncoder class.
From PHP, you can encode most objects to JSON using json_encode() (PHP5+). Using the AS3 JSON decoder from as3corelib you can then convert that encoded string into an oject that AS3 con read. After that you can iterate over the object as key/value pairs and output it however you wish.
I think I know what you mean, I had the same need, so I made this
http://www.actionscript.org/forums/showthread.php3?t=266979
scroll to the bottom, there is a description and an .AS file called trace_r that I wrote
I need to push some JSON data to my website which I would like to read in PHP. What type of file should I make this? A PHP file with the JSON inside of a variable? I understand how to make a text file with JSON encoded data in it, but how do I get this into PHP? Should I use a PHP include with the JSON-encoded data in it assigned to a variable? Or should I read the file from PHP and put the contents into a variable?
Save your json string as plain text, then you can use:
$file = yourfile
$data = file_get_contents($file);
$parsed = json_decode($data);
// compacted:
$parsed = json_decode(file_get_contents($file));
See file_get_contents() and json_decode().
The advantage of doing this (versus storing it in a PHP file then including it) is that now any program or language that understands JSON can read the file.
The question is too vague for a definite "do this" answer, but here are some options and what they might be most suitable for:
Turn the json data into a PHP data structure. If this is a one-time thing (meaning you won't be getting a new json file every day or week or hour), then reading a file (file_get_contents) and parsing JSON (json_decode) for every request is a pretty big waste of resources since that data isn't changing on a regular basis. Just turn JSON key/value objects into PHP associative arrays, JSON strings into PHP strings, etc.
Just serve the json file. If this is data that will just wind up going to the client to be used in javascript anyway, there's no need to do anything special with it on the server, just parse the json on the client.
Put it in a database. This may be a little heavy-handed, but if you really need it in PHP and not just the client, and it is going to be changing or growing on a regular basis, it may be worth it to have something that handles this use case appropriately.
I need to dump an xml string (coming into a web service as a POST param), directly into a Mysql DB. The column into which I am writing is currently of type 'text', though I have tried blob as well.
Right now, when I save the string and retrieve it later on using sql, it comes back in a serialized like format, like this:
a:1:{s:14:"<?xml_encoding";s:1502:"UTF-8?><record>
<nodes></nodes>
</record>";}
Whereas, I need to parse that xml as a simplexml object, and hence need it to be better formed.
This is what I am doing codewise:
Accept Post Param in web service
Serialize and store it in DB using doctrine ORM, kind of like
$record->xml_rec = serialize($_POST)
Retrieve it and echo it.
Oddly enough, if I unserialize and echo is upon retrial, I get an array. The array looks like this upon print_f
Array
(
[<?xml_encoding] => UTF-8?><record>
<nodes></nodes>
</record>
)
Still not parse-able by simplexml.
Is this a mysql storage problem or am I doing something wrong with the POST params? I've tried htmlspecialchars to save the string in the db, but similar problems remain.
any help is appreciated, thanks
What you have is a "serialized" php array that you can unserialize by using ... PHP's unserialize function.