Reputation: 419
i've got an JSON String like this:
a: 2: {
s: 4: "unit";
s: 2: "h1";
s: 5: "value";
s: 40: "Mercedes-Benz A 45 AMG / 340 PS / Allrad";
}
Now, I don't know what to do with that because of the weird keys (i think in the 's' that means the length of the string).
If I use it like this nothing can parse it (with php json_decode or with obj-c SBJsonParser). Is there a way to generate html of that in php or how can i read only the last value?
Thanks in advance
Upvotes: 0
Views: 299
Reputation: 4546
The above mentioned JSON format is absolutely invalid.
{
"a": {
"2": [
{ "s": { "4": "unit" } },
{ "s": { "2": "h1" } },
{ "s": { "5": "value" } },
{ "s": { "40": "Mercedes-Benz A 45 AMG / 340 PS / Allrad" } }
]
}
}
This would be a valid JSON format
Upvotes: 1
Reputation: 17930
This is not a valid JSON so no json parser would read it, you can treat it as a regular string and use string manipulation to get what you need.
In PHP in order to get the last value:
$str = ...//your json string
$lastValue = substr($str,strripos($str,'s:'));
this should return (according to your sample input): "40: "Mercedes-Benz A 45 AMG / 340 PS / Allrad";"
Upvotes: 2
Reputation: 14068
I suggest to treat it as a string for the beginning. Then use string manipulation to get rid of the "a: " and "s: ". Other requests may return other types (if I am right that these letters indicate a data type.) Then you could try parsing it with a JSON parser.
No guarantee though :) Because I am not qute sure out of the top of my head, whether these numbers are valid JSON or have to be encapsulated in quotation marks.
Upvotes: 0
Reputation: 3375
This looks like serialized array, but your string is definitely not valid. (http://php.net/manual/en/function.unserialize.php)
Upvotes: 3