开发者

PHP-Working with json

Iam working with json

My application get string that encoded by json_encode().

Example : The string like this {"data":"1", "data":"2", "d开发者_运维问答ata":"3"}

My app work well,

But if browser give me something before i get my string like this: skdjksdjksdj{"data":"1", "data":"2", "data":"3"}

My app will get all skdjksdjksdj{"data":"1", "data":"2", "data":"3"} and don't work

How i can take only {"data":"1", "data":"2", "data":"3"} without "skdjksdjksdj" ?

i don't want to use str_replace , i don't want to output something to browser before output string that was encode by json , and maybe aslo after that

sorry for my bad english

P/S:

Exampele: i addtion something like "bla bla bla" before json data

bla bla bla bla bla
myDataJson
bla bla bla bla bla

my app will get above content to paser , but catch error with "bla bla bla "

How to filter to get only myDataJson


I believe that only solutions here works this way - DO NOT OUTPUT ANYTHING BUT YOUR JSON STRING! You are the one who makes your app to output like anything. So you have to deal with this :)


You can strip the jSON before you do the json_decode() for example...

$json = "skdjksdjksdj{"data":"1", "data":"2", "data":"3"}"

strip the "skdjksdjksdj" how ever you like maybe like this...

Use strpos() to find where the {" is then do a substr() from that pos to the end of the string something like

substr(strpos("**{"**"), $json.length); 

then you can go and so whatever e.g

json_decode($json, 1)

ect

good luck!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜