PHP / AS3正则表达式来分割多JS​​ON在一个 [英] php/as3 regex to split multiple json in one

查看:120
本文介绍了PHP / AS3正则表达式来分割多JS​​ON在一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有这串2 JSON对象:

  

{词汇表:{称号:例如词汇表},AAA:1212} {阿达达:faka}

我想给它的数组PHP和ActionScript 3

在拆分

 阵列(
[0] => {词汇表:{称号:例如词汇表}
[1] => {阿达达:faka}
)
 

什么是做到这一点的最好方法。

编辑:

我不需要回答如何解析JSON。为了简化我需要拆分

  

{... {..} ....} {......} {} ........

  

{... {..} ....}

     

{....}

     

{........}

解决方案

或者修改一个JSON解析器来做到这一点,因为Amargosh建议,或者你可以做一个简单的算法来为你做,那skipps通过字符串和注释并跟踪开放括号。当没有开括号离开,那么你有一个完整的价值。重复它,直到你输入的。

不过我的建议是尝试通过交谈,谁负责的输出来解决这个问题,并说服他产生有效的JSON,这是 [{词汇表:{称号:例如词汇表},AAA:1212},{阿达达:faka}]

编辑:分离JSON对象,你需要preFIX每个JSON对象的长度(4字节就足够了)。那么你读出插槽,直到你有字符适量。下一字符将再次成为下一个对象的长度。这是你必须这样做,因为TCP工作在数据包。您不仅可以在一个单一的数据包的多个JSON对象,但你可以有两个包之间的一个JSON对象分割。

此外,一对夫妇的建议:

  • 请不要使用PHP插槽服务器。它不是为做。看看 haXe的,特别是猫后端。
  • 请不要写这样的东西,你自己,除非你真的需要。这是无聊和愚蠢的工作。几乎已经数以百万计的插槽服务器解决方案。你也应该看看 haXe的远程处理,让如之间的透明通讯一个Flash客户端和猫套接字服务器。同时,请看看smartfox和RED5。

EDIT2:你低估了问题,你的做法是不好的。你应该建立一个强大的解决方案,所以要通过网络发送阵列的一天,你会不会有一个彻底崩溃,因为你的分流休息,或JSON解析器被送到不完整的对象,因为只有一半的对象读。你想做的事,可以很容易地做了什么:采用分体式输入} {,追加}来任何元素,但最后和prePEND {的任何元素,但第一。但是我大量建议你从这样的做法避免,因为你会后悔的在一些点。如果你真的认为你应该做的认为,这样的你自己,然后尝试至少做他们的权利。

格尔茨
back2dos

For example I have this string of 2 json objects:

{"glossary": {"title": "example glossary"}, "aaa": "1212"}{"adada": "faka"}

I want to split it in the array for PHP and Actionscript 3

Array ( 
[0] => '{"glossary": {"title": "example glossary"}', 
[1] => '{"adada": "faka"}' 
)

What is the best method to do it.

Edit:

I don't need answer how to parse json. To simplify I need to split

{...{..}....}{....}{........}

into

{...{..}....}

{....}

{........}

解决方案

either you modify a JSON parser to do it, as Amargosh suggested, or you can make a simple algorithm to do it for you, that skipps through strings and comments and keeps track of open braces. when no open braces are left, then you have a complete value. repeat it until you're out of input.

however my suggestion is to try to solve the problem by talking to whoever is responsible for that output and convince him to generate valid JSON, which is [{"glossary": {"title": "example glossary"}, "aaa": "1212"},{"adada": "faka"}]

edit: to separate the JSON objects, you need to prefix every JSON object with a length (4 byte should suffice). then you read off the socket until you have the right amount of chars. the next characters will again be the length of the next object. it is imperative that you do this, because TCP works in packets. Not only can you have multiple JSON-objects in one single packet, but you can have one JSON-object split between two packets.

Also, a couple of advises:

  • do not use PHP for socket servers. it's not made for that. have a look at haXe, specifically the neko backend.
  • do not write this kind of stuff on your own unless you really need to. it's boring and dumb work. there are virtually millions of solutions for socket servers. you should also have a look at haXe remoting that allows transparent communication between e.g. a flash client and a neko socket server. also, please have a look at smartfox and red5.

edit2: you're underestimating the problem and your approach isn't good. you should build a robust solution, so the day you want to send arrays over the wire you won't have a total breakdown, because your "splitter" breaks, or your JSON parser is fed incomplete objects, because only half the object is read. what you want to do can be easily done: split the input using "}{", append "}" to any element but the last and prepend "{" to any element but the first. Nonetheless I heavily suggest you refrain from such an approach, because you will regret it at some point. if you really think you should do thinks like these on your own, then try to at least do them right.

greetz
back2dos

这篇关于PHP / AS3正则表达式来分割多JS​​ON在一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆