使用php解析类似json的数据

Parsing json like data using php

本文关键字:json 数据 php 使用      更新时间:2024-02-17

我有一些类似Json的数据从URL 中抓取

[[["oppl.lr",[,,,,,,,,,,,[[[[[,"A Google User"]
,,,,1]
,3000,,,"Double tree was ok, it wasnt super fancy or anything. Its good for families and  just relaxing by the pool. Service was good, and rooms were kept neat.","a year ago",["http://www.ma..",,1],,"","",""]
]
,["Rooms","Service","Location","Value"]
,[]

使用php json_decode()函数无法解析。有没有任何库或其他东西可以让我将其转换为常规json,这样我的任务就会更容易?否则我知道我必须写正则表达式。

提前感谢。

基于您的评论。如果您的数据是

[["oppl.lr",[,,,,,,,,,,,[[,"A Google User"],"",""],""]]]

如果你能以某种方式将数据发送到客户端。那么它就是一个有效的javascript数组。您可以在客户端处理数据,也可以使用

JSON.stringify([["oppl.lr",[,,,,,,,,,,,[[,"A Google User"],"",""],""]]]);

并将数据作为发送回服务器

"[["oppl.lr",[null,null,null,null,null,null,null,null,null,null,null,[[null,"A Google User"],"",""],""]]]"

否则,通过php您可以使用此函数

function getValidArray($input) {
   $input = str_replace(",,", ',"",', $input);
   $input = str_replace(",,", ',"",', $input);
   $input = str_replace("[,", '["",', $input);
   return eval("return $input;");
}

您可以根据需要优化上述功能。