使用 json_decode 从 json 获取字符串数组

     2023-02-16     58

关键词:

【中文标题】使用 json_decode 从 json 获取字符串数组【英文标题】:Get array of strings from json with json_decode 【发布时间】:2022-01-09 02:17:03 【问题描述】:

看起来很容易,但我没有得到预期的数据。我想将一个字符串数组发送到我的后端,然后对它们进行迭代/处理。

在前端我有:

var jsonArray = ["String1", "String2"]
await newFile(JSON.stringify(jsonArray));

在我的控制器中,我有:

$requestData = json_decode($request->getContent(), true); 
$this->logger->info("File request data is ", [ $requestData ]);

我的记录器输出:

File request data is ["[\"String1\",\"String2\"]"]

这不是一个数组,而是一个字符串。

如果我在 php 中使用

$txt = ["Test", "Test2"];
$json = json_encode($txt, true);

print_r(json_decode($json));

输出将是一个数组。我哪里错了或者我错过了什么?在 json_decode 中拥有 true 选项应该会返回我的数组。

【问题讨论】:

您是否尝试在 json_decode 方法中使用“false”? 好吧,如果你需要在后端解码两次意味着显然你已经在前端序列化了一次过多 【参考方案1】:

这个

["Test", "Test2"]

是一个数组,而这个

"[\"String1\",\"String2\"]"

是一个字符串。 json_decode() 需要一个字符串作为参数。

$jsonString = "[\"String1\",\"String2\"]";
$array = json_decode($jsonString, true);

var_dump($array);

/*
array(2) 
  [0]=>
  string(7) "String1"
  [1]=>
  string(7) "String2"

*/

【讨论】:

使用 json_decode 解码条带 json 不起作用

】使用json_decode解码条带json不起作用【英文标题】:decodingstripejsonwithjson_decodenotworking【发布时间】:2012-05-1708:50:46【问题描述】:我有来自条带的json,我正在尝试将它解码为json_decode。我没有收到错误消息。只是什么都没有回... 查看详情

jsonp -> json_decode()

】jsonp->json_decode()【英文标题】:【发布时间】:2011-01-0706:18:39【问题描述】:由于某种原因,我无法从返回的jsonp字符串中获取信息,<?php//CreatedbyTalisman01/2010★✩$vorto=$_GET[\'vorto\'];//GettheWordfromOuterSpaceandSearchforit!if(isset($vort... 查看详情

php中json_decode()和json_encode()的使用方法

json_decode对JSON格式的字符串进行编码而json_encode对变量进行JSON编码,需要的朋友可以参考下1.json_decode()json_decode(PHP5>=5.2.0,PECLjson>=1.2.0)json_decode—对JSON格式的字符串进行编码说明mixedjson_decode(string$json[,bool$assoc])接受一个JSON 查看详情

如何正确使用 JSON.stringify 和 json_decode()

】如何正确使用JSON.stringify和json_decode()【英文标题】:howtouseJSON.stringifyandjson_decode()properly【发布时间】:2013-04-0520:36:46【问题描述】:我试图通过以下方式将多维Javascript数组传递到我网站上的另一个页面:在数组上使用JSON.strin... 查看详情

nodejs怎么获取post请求的json数据

参考技术A前端传给服务器的只是json格式的字符串,使用json_decode,mixedjson_decode(string$json[,bool$assoc])var_dump(json_decode($json));var_dump(json_decode($json,true));如果不带参数,则返回为对象带上true时,返回数组foreach($ARRas$k=>$v)echo'key=... 查看详情

为啥在此 JSON 字符串上使用 json_decode 会导致空值

】为啥在此JSON字符串上使用json_decode会导致空值【英文标题】:Whydoesusingjson_decodeonthisJSONstringresultinanullvalue为什么在此JSON字符串上使用json_decode会导致空值【发布时间】:2021-08-2613:17:56【问题描述】:"op=add&item="firstName":"test",... 查看详情

PHP json_decode() 使用有效的 JSON 返回 NULL?

】PHPjson_decode()使用有效的JSON返回NULL?【英文标题】:PHPjson_decode()returnsNULLwithvalidJSON?【发布时间】:2011-01-2512:05:52【问题描述】:我将此JSON对象存储在纯文本文件中:"MySQL":"Server":"(server)","Username":"(user)","Password":"(pwd)","DatabaseNa... 查看详情

php使用json_decode解析json返回null

<?phpheader('content-type:text/html; charset=utf-8');$member_info=$_POST['mypostdata'].trim();$encode = mb_detect_encoding($member_info, array("ASCII","UTF-8 查看详情

如何将json数据从angular js传递到php

...否将json数据正确传递给PHP?我已经使用$_POST、$_REQUEST和json_decode来获取数据。没有任何效果。如何在PHP中获取json数据?控制器.jsif(u 查看详情

PHP:JSON 中的正则表达式导致 json_decode 出现问题? [复制]

】PHP:JSON中的正则表达式导致json_decode出现问题?[复制]【英文标题】:PHP:regexinJSONcausesproblemforjson_decode?[duplicate]【发布时间】:2019-06-1306:43:47【问题描述】:我的JSON中有一些正则表达式,当我在在线JSON验证器上测试我的JSON时... 查看详情

CodeIgniter:json_decode 数组问题

】CodeIgniter:json_decode数组问题【英文标题】:CodeIgniter:json_decodearrayissues【发布时间】:2011-02-1904:35:20【问题描述】:在我的客户端,我正在使用jQuery发送一个ajax请求:$.post(script.php,"var1":"something","var2":"[1,2,3]",function(data),"json");... 查看详情

使用 Powershell 解析从 Web 请求返回的 JSON 字节流而不写入文件

】使用Powershell解析从Web请求返回的JSON字节流而不写入文件【英文标题】:ParsingaJSONByteStreamReturnedfromaWebRequestusingPowershellwithoutwritingtoafile【发布时间】:2022-01-1209:59:40【问题描述】:我正在使用以下Powershell调用返回JSON文件的URL... 查看详情

在 PHP json_decode() 中检测错误的 json 数据?

】在PHPjson_decode()中检测错误的json数据?【英文标题】:DetectbadjsondatainPHPjson_decode()?【发布时间】:2011-01-2119:59:43【问题描述】:当通过json_decode()解析时,我正在尝试处理错误的json数据。我正在使用以下脚本:if(!json_decode($_POST)... 查看详情

json_decode() (PHP 7) 中的新行和标签

】json_decode()(PHP7)中的新行和标签【英文标题】:Newlinesandtabsinjson_decode()(PHP7)【发布时间】:2016-04-0121:06:12【问题描述】:我使用json_decode()的代码在PHP5.6中正常工作。迁移到PHP7.0后,json_decode()返回NULL,json_last_error()告诉我我的错... 查看详情

tp中对json_decode返回null的解决办法

json_decode要求的字符串比较严格:(1)使用UTF-8编码(2)不能在最后元素有逗号(3)不能使用单引号(4)不能有 , ,如果有请替换tp框架中的I函数默认对post的参数进行了htmlspecialchars过滤‘DEFAULT_FILTER‘=>‘htmlspecialchars‘,//默... 查看详情

如何在 Javascript 中使用 file_get_contents 和 json_decode

】如何在Javascript中使用file_get_contents和json_decode【英文标题】:Howtousefile_get_contentsandjson_decodeinJavascript【发布时间】:2013-02-0912:58:11【问题描述】:我正在尝试在Javascript中使用这个PHPAPI。?​​p>PHPAPI代码$content=@file_get_contents(... 查看详情

从 jQuery Ajax 发布时 PHP Json 格式错误

...一个奇怪的问题,当我对来自jqueryajax调用的某些json使用json_decode()时,它总是说json格式错误(JSON_ERROR_SYNTAX)。我说这很奇怪,因为如果我从开发人员控制台获取原始发布的jso 查看详情

hp数组转json函数json_encode和json转数组json_decode函数的使用方法

这两个函数比较简单,我这里直接写例子,但是有一点一定要注意,json数据只支持utf-8格式,GBK格式的数据转换为json会报错!json_encode()用法:<?php$data=array(‘name’=>’jianqingwang’,‘sex’=>’man’,&l... 查看详情