How would I parse this or convert this into JSON? How would I parse this or convert this into JSON? json json

How would I parse this or convert this into JSON?


No. It is not possible to automatically parse a response if you don't know the language it uses.

While the response looks like JSON, it isn't. It's not even close to JSON. You can't just quote the keys to make it valid JSON.

Except for the revisionDate, it seems to be valid Javascript, but who knows?

The parser needs to know every data type it might potentially encounter. There's no telling what could appear in the response. Unless you find the documentation for this format, you never know what you might encounter.

You could in theory be able to parse the language you think this response is. But

  1. you need to implement your own parser. That's not easy. At all.
  2. you cannot assume you know the responder language. This response is one value from being valid javascript.
  3. a poorly implemented parser will not even know it met something it doesn't understand. Instead of giving up, it will produce unexpected results
  4. if you modify your model of the language, if your parser isn't a full-blown parser, incorporating the change into the parser could easily lead to a complete rewrite of the parser.

Conclusion:

You don't know the language the responder talks in. This means you cannot parse it. Find the documentation first, then talk about parsing.


If you're using Flash/AS3 to receive this packet, you could use the as3corelib JSONDecoder Class.

Here is an example using the JSON sample you provided:

import com.adobe.serialization.json.JSON;var raw:String = "{ name: 'com.riotgames.platform.summoner.PublicSummoner', keys: [ 'internalName', 'dataVersion', 'acctId', 'name', 'profileIconId', 'revisionDate', 'revisionId', 'summonerLevel', 'summonerId', 'futureData' ], object: { internalName: 'mrquackers', dataVersion: 0, acctId: { value: 34117327 }, name: 'MrQuackers', profileIconId: 502, revisionDate: Tue, 30 Oct 2012 19:38:32 GMT, revisionId: { value: 0 }, summonerLevel: { value: 30 }, summonerId: { value: 20933307 }, futureData: null }, encoding: 0 }";// 1. Add quotes to all keys. 2. Wrap string around date object. 3. Replace single quotes with double quotes.raw = raw.replace(/([\w]+): /g, "'$1': ").replace(/\w+, \d* \w+ \d* \d*:\d*:\d* \w+/g, "'$&'").replace(/'/g, "\"");var json:Object = JSON.decode(raw, false);var date:Date = new Date(json.object.revisionDate);trace(json.name); // "com.riotgames.platform.summoner.PublicSummoner"trace(date.month); // 9


Here's what I would do. The only issue here is with the revisionDate, which would better be converted into a timestamp to be processed by ActionScript (or into a string, if you like) so I would use the preg_replace_callback() function to change it for the former condition or the preg_replace() function for the latter condition:

// this is for numeric timestamp: 1351625912$str = preg_replace_callback('~(?<=\srevisionDate:\s).+?(?=,\n|,\r|,\r\n)~',create_function('$m','return strtotime($m[0]);'),$str);// alternatively, you can wrap it with single quotes: 'Tue, 30 Oct 2012 19:38:32 GMT'//$str = preg_replace('~(?<=\srevisionDate:\s).+?(?=,\n|,\r|,\r\n)~',"'$0'",$str);echo $str;