This is my 3.txt file:
I don't know why it works when it has only 1 data and when it has more data it's not working.
This is my code:
Your 3.txt does not contain a valid json. You need to adjust your sample string to be valid JSON by adding quotes around strings, commas between objects and placing the objects inside a containing array (or object).
It should be something like this,
[{"address":"+6161616","body":"asda","date":"1231"},
{"address":"+616sdfs6","body":"as3sda","date":"155231"},
{"address":"+6161616","body":"asda","date":"123551"}]
You can test if a json is valid here.
Related
I made a post in a form converting my javascript localstorage to a post request. From there I tried to decode my json string to make an object in PHP.
How my php code looks before I echo it
$cart_items = $_POST['cart_items'];
$cart_items = json_encode($cart_items);
$array_test = json_decode($cart_items);
print_r($array_test);
What it returns in browser
[{\"id\":83494890,\"title\":\"2020 Hino 358\",\"partType\":\"Bumpers\",\"price\":100,\"stockNumber\":12313131312,\"thumbImg\":\"/jOIY91KhEby8_f.jpg\",\"permalink\":\"/part-description/?part=83494890\",\"maxQuantity\":1,\"requestedQuantity\":\"3\"}
,{\"id\":83493833,\"title\":\"2009 Freightliner 5020080\",\"partType\":\"ABS Modulator Valves\",\"price\":150,\"stockNumber\":\"P-1211111111157\",\"thumbImg\":\"/OOjQbsi6p8kX_f.jpg\",\"permalink\":\"/part-description/?part=83493833\",\"maxQuantity\":1,\"requestedQuantity\":\"1\"}]
I know that typically when seeing json data there isn't forward slashes everywhere. I tried to json_decode into an array rather than an object, then make a foreach for each object inside. But I got this error returned "Invalid argument supplied for foreach()"
How do I make this json string convert to an array of objects? Thank you
The problem I was having was when I was getting the $_POST[] it was using PHP's "magic quotes" which was giving me improper format for my json. That being said, after disabling this, it removes the slashes.
It looks like $_POST['cart_items'] already contains JSON. So you just need to decode it, not encode it first.
$array_test = json_decode($_POST['cart_items'], true);
print_r($array_test);
But it's actually encoded twice, that's why it has escaped quotes, so you need to call json_decode() twice. But it's missing the double quotes around the whole thing, and the embedded newline is not valid.
The following works:
<?php
$cart_items = '"[{\"id\":83494890,\"title\":\"2020 Hino 358\",\"partType\":\"Bumpers\",\"price\":100,\"stockNumber\":12313131312,\"thumbImg\":\"/jOIY91KhEby8_f.jpg\",\"permalink\":\"/part-description/?part=83494890\",\"maxQuantity\":1,\"requestedQuantity\":\"3\"},{\"id\":83493833,\"title\":\"2009 Freightliner 5020080\",\"partType\":\"ABS Modulator Valves\",\"price\":150,\"stockNumber\":\"P-1211111111157\",\"thumbImg\":\"/OOjQbsi6p8kX_f.jpg\",\"permalink\":\"/part-description/?part=83493833\",\"maxQuantity\":1,\"requestedQuantity\":\"1\"}]"';
$array_test = json_decode(json_decode($cart_items));
print_r($array_test);
I suggest you find the code that's sending the cart_item POST parameter and fix it so it doesn't do all this extra encoding.
I need to use a customer's API for loading JSON which only contains something like:
{"html" : "foo"}
The API is being used from other services so I'm pretty sure it's valid.
However, when trying to decode it using json_decode i'm always getting an empty string which means it's not valid. I found out i need to "fix" the JSON-String by replacing:
$json = str_replace("\\>", "\\\\>", $json); // \> = invalid json
It works mainly on each request but not on certain others but it's very tricky to debug and i can't imagine that replacing is the proper method.
How would i do it the easy way for converting the json string into a valid one?
thanks
Ok i could find out what's wrong:
The HTML contains backslashes in the closing tags, for example <br\>
You need to replace them like this:
$json = str_replace("\\>", "\\\\>", $json);
and json_decode will work
I have a field created by word press which contains data with the following structure.
a:16:{s:7:"country"
s:14:"United Kingdom"
s:7:"form_id"
s:2:"35"
s:9:"timestamp"
s:10:"1560869327"
s:7:"request"
s:0:""
s:8:"_wpnonce"
s:10:"125"
s:16:"_wp_http_referer"
s:1:"/"
s:17:"ajaxy-umajax-mode"
s:8:"register"
s:10:"first_name"
s:5:"xxxxx"
s:9:"last_name"
s:5:"xxx"
s:10:"user_email"
s:28:"xxx#xxx.co.uk"
s:7:"Company"
s:16:"xxx LTD"
s:12:"phone_number"
s:10:"0123456789"
s:8:"user_url"
s:20:"http://www.test.com"
s:15:"company_address"
s:18:"999 LockSmith Lane"
s:12:"display_name"
s:12:"XXXX"
s:10:"user_login"
s:10:"xxx123"
}
I want to convert this to an array so I can read the properties of it.
I tried converting it to json but its not json.
Any ideas on how I can parse this data, or access its properties in PHP.
I can not access this data through wordpress as my PHP script is part of something else.
Seems like a serialized array. Try unserializing it to convert it back to normal, see example.
This is Actually not a json.This is an array in serialized format you can just unserilize it by using this function
maybe_unserialize($YOUR_ARRAY).
maybe_unserialize is a wordpress default function to unserialize the array
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 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.