object(stdClass)#43 (3) { ["#promo"]=> string(4) "true" ["#rate"]=> string(6) "308.69" ["#baseRate"]=> string(6) "342.99" }
The above is the partial output from a JSON file. The original file is actually an XML file.
<NightlyRate promo="true" rate="227.09" baseRate="283.86"/>
I used jsondecode to convert the XML to json. However, I don't know how to get the values for each of the attribute.
Ex, i want to get 227.09 and 283.86 using JSON.
Thanks so much for your time.
I do find the entire process a bit odd, but you can access invalid variable name properties of stdClass using this syntax:
$stdClassVar->{"#rate"};
Related
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 have a PHP associative array that I am trying to extract data from:
array(18) { ["body"]=> string(34) "Hey! Let me know if you got this"}
The above array is stored in a variable called $firstChildData, and when I try to run the following line, I get the result below it:
$firstChildBody = $firstChildData["body"];
This returns: string(34) "Hey! Let me know if you got this"
Does anyone know how to remove the 'string(34)' so I can just have the value within the quotes? I have tried to use the explode() function with " as the delimiter, but that didn't work.
Thanks in advance!
string(34) is just a debug information if you use a function like var_dump or print_r.
You can print strings normal with echo or print.
echo $firstChildData["body"];
If you want to use the variable $firstChildData["body"] in any program code, it is handled as the string in it. The information string(34) ist not included.
Thanks everyone!
You're right, I was using var_dump, which was showing that information even thought it was not part of the string. I used echo instead and it works beautifully.
I know a lot of questions have addressed this issue but I have not found one that helped me...
When I parse json data downloaded from a localhost (MAMP server), I face the json error 3840 stating invalid value around character 0...
I don't understand why, since my php script with a var_dump on my array displays (an array of array):
array(2) { [0]=> array(5) { ["ID"]=> string(1) "1" ["EDS"]=> string(4) "1000" ["lastname"]=> string(8) "My lastname" ["firstname"]=> string(9) "My firstname" ["dateOfBirth"]=> string(10) "19.12.1975" } [1]=> array(5) { ["ID"]=> string(1) "2" ["EDS"]=> string(4) "1001" ["lastname"]=> string(14) "Smith" ["firstname"]=> string(6) "John" ["dateOfBirth"]=> string(10) "11.11.1111" } }
...which for me seems to be a valid json array.
When I log the NSMutableData downloaded, it is not null, but something like
76353648 2734b0a9 (+ around fifty like this).
I don't know if it is because the data is not complete, but I don't know how I can keep on analyzing a little further what is going wrong.
If anyone has an idea of what happens (I understand it has to do with a special character not recognized), it would be great.
Thanks a lot!
Edit: Added followup code to the original question:
In
(void)connectionDidFinishLoading:(NSURLConnection *)connection {
id jsonObject = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];
if ([jsonObject isKindOfClass:[NSArray class]]) {
NSArray *deserializedArray = (NSArray *)jsonObject;
for (NSDictionary *jsonElement in deserializedArray)
{
Person *newPersonElement = [[PersonStore sharedStore] createPerson]; // --> what makes the app crash. But this method is working everywhere else...
// more code omitted
}
I don't know why this initialization crashes here...
Updated answer:
I think now that you have posted the Objective-C code that uses this received JSON, it becomes clear what the actual problem is.
It sounds to me that you are using Core Data (PersonStore) to persist your incoming data.
If you are making Core Data calls from the same thread that connectionDidFinishLoading: is being called on, you are most likely running into a threading issue where Core Data is not happy that you call it from a thread other than the main thread.
Give this a try: in your connectionDidFinishLoading: wrap your code in the following:
dispatch_async(dispatch_get_main_queue(), ^{
// Include the code here that walks over the incoming JSON
// and creates new `Person` instances.
});
This will execute everything in that block, on the main thread. Which is generally a good idea for Core Data usage. (Probably even required, check the documentation, there is a special section on Core Data & Threads if I remember correctly).
Very curious if this does the trick.
Old answer:
The output of vardump is not actually valid JSON. You need to use the json_encode() function instead.
You can turn the NSData that you received from the server into a string by doing something like this:
if let s = String(data: data, encoding: NSUTF8StringEncoding) {
println(s)
}
You did not mention if you work in Swift or Objective-C but the above translates easily to Objective-C.
I have a PHP script with this content:
$request_data='teststring';
var_dump($request_data);
$bd = base64_decode($request_data);
var_dump($bd);
die()
When I load the page, it outputs
string(10) "teststring" string(7) "��-���"
But, if I pass a GET string to the page - any string at all - it outputs this instead:
string(10) "teststring" string(7) "µë-²Úâž"
What is going on? How can passing a GET string to the page change the functionality of base64_decode? If I set $request_data to valid base64 encoded data, then the GET argument does nothing, and it decodes it correctly. But when it's an invalid string, it decodes it differently depending on the argument. Why is this? How can I fix it?
I am trying to pass in base64 encoded encrypted data, which sometimes decodes to non-ascii characters, and am finding that when that happens I get the wrong values back. I need a way to safely base64_decode any data, whether or not it is an alphanumeric string.
I am building a request in Flash that contains a JSON encoded Array of Objects. This is encoded using as3corelib. The request is then passed to JavaScript via ExternalInterface and a jquery ajax call sends the request off to the server.
In PHP, the incoming data is parsed, but the json_decode returns null, giving a Malformed JSON error. var_dump results in comments:
<?php
(isset($_POST['gdata']) && !empty($_POST['gdata'])) ? $gamedata = $_POST['gdata'] : returnError("game data not specified");
var_dump($gamedata); // (String) = string(37) "[{\"duration\":1,\"id\":\"game2\"}]"
$gamedata = json_decode(utf8_encode(trim($gamedata)),true);
var_dump($gamedata); // null
$gamedata = json_decode("[{\"duration\":1,\"id\":\"game2\"}]",true);
var_dump($gamedata);
/*
array(1) {
[0]=>
object(stdClass)#1 (2) {
["duration"]=>
int(1)
["id"]=>
string(7) "game2"
}
}
*/
?>
What I don't understand is that attempting to decode the variable returns null, but the same text decoded from a literal string works fine. What can I do to clean up the incoming data and make it readable for json_decode?
Edit: php_info() says that magic_quotes_gpc is enabled. Could that be the issue?
magic_quotes_gpc could be the issue, yes. And if you re-encode blindly w/o knowing the charset could be an issue as well.
So if you know magic_quotes_gpc is enabled, you need to strip slashes first.
For the charset, take care you know in which charset the incomming data is encoded, not that it's already utf-8 encoded and you assume it's latin-1 and convert it again.