【问题标题】:Parsing json like data using php使用php解析json之类的数据
【发布时间】:2012-07-24 08:03:25
【问题描述】:

我有一些类似 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 以便我的任务更容易?否则我知道我必须写正则表达式。

提前致谢。

【问题讨论】:

  • 这是一个数组吗?你能正常化吗?表示类似array(name, array(work, array(something)) 的意思。因为从您的 OP 中的日期无法有意义地推断出任何内容
  • 这就是问题所在......我已经粘贴了我得到的实际数据......我也无法弄清楚它是如何格式化的?
  • 它甚至没有正确嵌套。如果您想提取数据,正则表达式是唯一的解决方案。您实际上想从中提取什么?
  • 如果 [["oppl.lr",[,,,,,,,,,,,[[,"A Google User"],"",""],""]] ] 是数据......那么有可能吗?我想为我的项目获取这些数据。
  • 可能是疯了,但是 Ya 这是一个有效的 javascript 数组:P 尝试 console.log([["oppl.lr",[,,,,,,,,,,,[[,"A Google User"],"",""],""]]]) 然后你可以使用 for..in 类型的循环。或json encode 相同并将其发送回您的服务器

标签: php javascript json web-crawler


【解决方案1】:

根据您的评论。如果您的数据是

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

如果你能以某种方式将数据发送到客户端。然后它是一个有效的 javascript 数组。您可以处理数据@client 端或使用

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;");
}

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

【讨论】:

  • 在 getValidArray 函数中的前两行是相同的......这有什么需要......?
  • 你输入了什么?我只测试了你的输入。我已经明确提到了You can optimize the above function as per the need,但这只是一个想法。因此,如果您有不同的输入,请考虑重写。因为你的 OP 清楚地提到了给出任何想法;)
  • 你上面提到的是输入。反正我会自己试试。谢谢 4 时间。
  • 如果它真的对您有帮助,请考虑支持或接受 :) 我的输入是 '[["oppl.lr",[,,,,,,,,,,,[[,"A Google User"],"",""],""]]]' 确保输入周围有单引号。
  • 不起作用...不需要eval函数...我在我的问题中也提到了是否需要编写正则表达式...
猜你喜欢
  • 2013-11-15
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 2019-03-05
  • 1970-01-01
  • 2021-05-06
相关资源
最近更新 更多