我有一个看起来像这样的代码
""" HTTP/1.0 200 OK\r\n Cache-Control: no-cache\r\n Content-Type: application/json\r\n Date: Tue, 08 Nov 2002 05:46:42 GMT\r\n \r\n {\n "request": {\n "body": ""\n },\n "response": {\n "status": 1,\n "users": [\n {\n "user_id": 1,\n "username": "john.doe",\n "account_status": "1",\n "online_status": 1,\n } ]\n }\n } """这个值来自一个数据库,我唯一的问题是,我无法使用json_decode解码它...是否有任何类或函数可以解码它转换为array()?
I have a code which looks like this
""" HTTP/1.0 200 OK\r\n Cache-Control: no-cache\r\n Content-Type: application/json\r\n Date: Tue, 08 Nov 2002 05:46:42 GMT\r\n \r\n {\n "request": {\n "body": ""\n },\n "response": {\n "status": 1,\n "users": [\n {\n "user_id": 1,\n "username": "john.doe",\n "account_status": "1",\n "online_status": 1,\n } ]\n }\n } """That value came from a database, the only problem I've got is that, I can't decode it using json_decode... Are there any class or function that can decode this to convert into array()?
最满意答案
正如我在评论中已经提到的那样,你所拥有的是一个完整的HTTP响应。 它将JSON数据保存在正文中,因此您需要解析它。 这是一种方法:
preg_match("/\{(.*)\}/s", $rawResponse, $matches);这匹配了第一个“{”和最后一个“}”之间的所有内容,这是JSON数据所在的位置,并将每个匹配项存储在$matches 。 由于只应该有一个匹配,我们对$matches[0]感兴趣。
现在你只需要解码它:
$data = json_decode($matches[0]); // or // $data = json_decode($matches[0], true) // if you want an associative array instead of an object.警告:这不适用于您的示例,因为它不是有效的JSON。 "online_status": 1,之后的逗号"online_status": 1,使其无效,因此json_decode将返回false 。 但我猜你刚刚在问题中删除了一些内容以提高可读性。
如果您可以控制存储内容的方式,那么您一定要考虑只存储JSON数据,这样您就不必处理这些问题。 如果你仍然需要整个原始响应,可能将它存储在另一个表/列中。 这使得事情变得更加容易,特别是对于Laravel。 例如,如果数据类似于Transaction模型上的details字段,则可以执行以下操作:
class Transaction extends Model { protected $casts = [ 'details' => 'json' ]; }现在,当访问$transaction->details您将自动获得一个数组。 这甚至可以在两个方向上工作: $transaction->details = ['new' => 'data']会在保存之前将其转换为JSON。 所以你根本不需要处理来回转换。
As already mentioned in my comment, what you have there is a whole HTTP response. It holds the JSON data in the body, so you need to parse that. Here's one way to go about it:
preg_match("/\{(.*)\}/s", $rawResponse, $matches);This matches everything between the firs "{" and the last "}", which is where the JSON data lives, and stores every match in $matches. Since there should only be one match, we're interested in $matches[0].
Now you only need to decode it back:
$data = json_decode($matches[0]); // or // $data = json_decode($matches[0], true) // if you want an associative array instead of an object.Caveat: This won't work with your example, because it's not valid JSON. The comma after "online_status": 1, makes it invalid so json_decode will return false. But I guess you just stripped some lines in your question for better readability.
If you have control over how things are stored, you should definitely consider only storing the JSON data so you wouldn't have to deal with such issues. If you still somehow need the whole raw response, maybe store it in another table / column. This makes things a lot easier, especially with Laravel. For instance, if the data is something like a details field on a Transaction model or something, you could do someting like this:
class Transaction extends Model { protected $casts = [ 'details' => 'json' ]; }Now, when accessing $transaction->details you'll automatically get an array. And this even works in both directions: $transaction->details = ['new' => 'data'] will convert it to JSON before saving it. So you wouldn't have to deal with converting back and forth at all.
更多推荐
发布评论