0

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

MGubler
  • 369
  • 4
  • 15
  • 6
    This is obviously not JSON. Do you have access to spec/manual of your data source to check what it really is? – Oleg V. Volkov Jul 10 '12 at 14:12
  • This is no JSON of the book. It is some custom stucture similar to JSON. I assume that the a: indicates that the following object is an array and the s: indicates that the following data is of string type. Just a guess of course. – Hermann Klecker Jul 10 '12 at 14:14
  • thanks, i though, that this isn't valid, but a servicepartner delivered it to me and i have to parse it. normal stringmanipulation seems not to be very easy for that. This string is (i think so) generated from the contao cms and the cms will rewrite it to html, unless I am mistaken so has anybody an idea how i could handle this – MGubler Jul 10 '12 at 14:28

4 Answers4

3

This looks like serialized array, but your string is definitely not valid. (http://php.net/manual/en/function.unserialize.php)

Eugene
  • 3,280
  • 21
  • 17
2

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";"

Tomer
  • 17,787
  • 15
  • 78
  • 137
1

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

basvk
  • 4,437
  • 3
  • 29
  • 49
0

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.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71