开发者

Parsing JSON like data interchange format

I'd like to use a data interchange format that uses no quotation marks. Maybe something based on JSON:

{
  param: value,
  p开发者_JAVA技巧aram: value,
  param: {
    [{
      param: value,
      param: value
    }, {
      param: value,
      param: value
    }]
  }
}

How should I go about parsing something like that in let's say PHP. Should do it through regular expressions?


What's so important about having your format not use quotation marks? Chances are there's something else you could change to make that requirement go away (which would be better).

As far as using regular expressions to parse JSON-like formats, no. This is a BAD idea; regular expressions were never really intended to parse recursive structures like JSON. At the very least you'll run into issues with performance of the many regex matches you'll need to use to attempt to deal with recursion; at worst you'll run into snafus of attempting to even sort out how to match and parse recursion in the first place.

Your format as proposed has its own issues, as well: how do you differentiate between a ,, {, or : in the value of a key, and the actual ,, {, or : that's part of the format? How do you deal with spaces in key or value names?


Why not use JSON and leverage off the available libraries and tools available to you ? This blog entry details a JSON parser in PHP.

To handle JSON data there is JSON extension in PHP which is available after PHP 5.2.0. Two functions : json_encode() and json_decode() are very useful converting and parsing JSON data through PHP.

Creating another format seems repetitive and error prone when there are so many well-defined and well-tested options available (JSON, XML, Google Protocol Buffers, YAML).


You could use XML and not use any attributes. You'd also need to avoid the header, which has some quotation marks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜