修剪JSON值中的前导和尾随空格 [英] Trim leading and trailing white spaces in JSON values

查看:93
本文介绍了修剪JSON值中的前导和尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我使用PHP向Web服务器发出JSON请求,它在变量中返回了JSON响应. JSON响应将包含许多键和值.我想要一个为整个JSON响应修剪键值对的每个值"中前导和尾随空格的函数.

I make a JSON request to a web server using PHP and it returns me a JSON response in a variable. The JSON response will have lots of keys and values. I would like a function which trims leading and trailing white spaces in each "value" of the key-value pair for the entire JSON response.

如何通过PHP做到这一点?

How can I do that through PHP?

例如:json_decode由于尾随空格或特殊字符而中断:

Ex: json_decode breaks due to trailing spaces or special characters:

{
    "glossary": {
        "title": "example glossary",
  "GlossDiv": {
            "title": "S",
   "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
     "SortAs": "SGML",
     "GlossTerm": "Standard Generalized Markup Language‡flfi·€..  ",
     "Acronym": "SGML",
     "Abbrev": "ISO 8879:1986",
     "GlossDef": {
                        "para": "create markup languages such as DocBook.     ",
      "GlossSeeAlso": ["GML", "XML"]
                    },
     "GlossSee": "markup"
                }
            }
        }
    }
}

推荐答案

在将数据编码为JSON格式之前对其进行处理.与弄乱JSON版本相比,清理源代码更好,并且可能使用格式不正确的正则表达式破坏语法,删除不应该包含的内容.

Process the data BEFORE it's encoded into JSON format. Better to clean up the source than mess with the JSON version and possibly break the syntax with a malformed regex deleting something it shouldn't have.

基本上,请执行以下操作:

Basically, do this:

foreach($data as $key => $value) {
    $data[$key] = trim($value);
}

$json = json_encode($data);  // $json's values are now pre/post-whitespace free

(假设它是一个简单的一维数组).

(assuming it's a simple 1-dimensional array).

编辑/评论跟进:

您的PHP脚本是否正在获取此外部JSON?如果是这种情况,那么您可以将JSON轻松解码为PHP对象/数组,进行空白修剪,然后重新编码为JSON:

Is your PHP script fetching this external JSON? If that's the case, then you can trivially decode the JSON into a PHP object/array, do the whitespace trimming, and re-encode into JSON:

$json = get_json_from_external_source();
$data = json_decode($json);

,然后像以前一样执行foreach循环(或Tomalak的评论中提到的array_map).如果您仅限于使用Javascript进行此客户端,则可以在将数据交给任何需要它的功能之前,在该处进行等效处理.

and then the foreach loop (or array_map as mentioned in Tomalak's comment) as before. If you're limited to doing this client-side in Javascript, then you can do the equivalent processing there before handing the data over to whatever function requires it.

编辑/评论跟进#2:

我非常怀疑这是JSON数据值内的尾随空格. JSON完全有能力处理字符串中出现的空格,而不管存在多少(或很少).很有可能是GlossTerm条目中的时髦字符.

I highly doubt it's the trailing spaces inside the JSON data's values. JSON is perfectly capable of handling spaces wherever they occur within a string and doesn't care how many (or few) there are. Most likely it's the funky characters in the GlossTerm entry.

如果您使用的是PHP 5.3(或更高的Beta版本),则

If you're on PHP 5.3 (or a higher beta version), there's json_last_error() which will report as to why the decode's failing.

这篇关于修剪JSON值中的前导和尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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