【问题标题】:Filter a JSON out of a string for further processing using php从字符串中过滤出 JSON 以便使用 php 进行进一步处理
【发布时间】:2018-03-01 08:12:39
【问题描述】:

在我的应用程序中,我调用了一个返回 JSON 的 API,不幸的是,这个 JSON 被一个带有 HTML 元素的字符串包围

示例返回

{"code":[{"name":"code","text":null,"attributes":[],"children":[]}],"message":[{"name":"message","text":"An error occurred while processing this request.","attributes":[],"children":[]}],"innererror":[{"name":"innererror","text":null,"attributes":[],"children":{"message":[{"name":"message","text":"Entiteit: Validatiefout","attributes":[],"children":[]}],"type":[{"name":"type","text":"System.Exception","attributes":[],"children":[]}],"stacktrace":[{"name":"stacktrace","text":"at Exact.Services.REST.DataServiceUpdateProvider.ErrorException(FaultException`1 fe, IExactRestConnection connection)\r\n   at Exact.Services.REST.DataServiceUpdateProvider._Closure$__6._Closure$__7._Lambda$__6()\r\n   at Exact.Services.REST.DataServiceUpdateProvider.SaveChanges()\r\n   at System.Data.Services.DataService`1.HandleNonBatchRequest(RequestDescription description)\r\n   at System.Data.Services.DataService`1.HandleRequest()","attributes":[],"children":[]}],"internalexception":[{"name":"internalexception","text":null,"attributes":[],"children":{"message":[{"name":"message","text":"Property: Ongeldige referentie, Message: Artikelcode: 'EY05547'","attributes":[],"children":[]}],"type":[{"name":"type","text":"System.Exception","attributes":[],"children":[]}],"stacktrace":[{"name":"stacktrace","text":null,"attributes":[],"children":[]}]}}]}}]}<div class="debugmsgs"><div class="debugtitle"><span id="debugtitlename">Debugger</span> | <a id="debugtitlename2">Autofill</a> <div class="debugtitle-link"><a id="debugtitle-a" href="#" onclick="toggleDebug(); return false;">inklappen</a></div></div><div class="debugerrors"><div class="debugerror debugerror-php">Undefined index: content<br /><em>C:\laragon\www\b-</div><div class="debugerror debugerror-php">Undefined index: content<br /><em></em><br /></div></div></div>

正如您在 json 中看到的那样:

:[],"children":[]}]}}]}}]}<div class="debugmsgs">

Html 附加到字符串中,这导致我无法进行 JSON 解码。 因此我需要删除 HTML,或者提取 JSON。

我想做的是将返回的字符串中的 JSON 放入一个变量中以供进一步处理。

到目前为止,如果尝试使用正则表达式执行此操作:{(?:[^{}]|())*},但我似乎无法正确处理。

【问题讨论】:

  • json 格式是否正确?尝试使用json_decode 然后获取数组并解析
  • 感谢您的提问,JSON 附加了一些 HTML 标签,因此无法解码 JSON。这实际上是我无法进一步处理 JSON 的原因

标签: php json regex


【解决方案1】:

您的 json 格式似乎是错误的,因为与输出一起生成了某种调试代码,解决此问题的正确方法是找出导致下面的调试消息出现的原因

<div class="debugmsgs"><div class="debugtitle"><span id="debugtitlename">Debugger</span> | <a id="debugtitlename2">Autofill</a> <div class="debugtitle-link"><a id="debugtitle-a" href="#" onclick="toggleDebug(); return false;">inklappen</a></div></div><div class="debugerrors"><div class="debugerror debugerror-php">Undefined index: content<br /><em>C:\laragon\www\b-</div><div class="debugerror debugerror-php">Undefined index: content<br /><em></em><br /></div></div></div>

如果您无法摆脱输出,您可以使用的其他方法是 PHP 爆炸以删除不需要的字符串,如下所示:

$string = explode("<div", $api_output); //break the string into array using the '<div' as breaking point
$result = json_decode($string[0]); //decoded version of the json output

【讨论】:

  • 感谢您的回答,(+1) 这确实是问题所在,但是,由于这是来自外部 API 的返回,我无法访问产生此错误的代码。
  • 那么从长远来看,运行此 API 会很棘手,如果您无论如何都无法联系 API 开发人员来解决问题,那么唯一的方法是使用正则表达式删除任何内容在您的输出中的 } 之后
  • 我将如何形成这个正则表达式?
猜你喜欢
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
相关资源
最近更新 更多