首页 新闻 会员 周边

json_encode 没有把数组转为json

0
悬赏园豆:50 [已解决问题] 解决于 2020-09-15 15:17

做了一个接口对接的数据传输,对方需要的是json数据
但是返回错误

"JSON parse error: Cannot deserialize instance of java.util.ArrayList out of VALUE_STRING token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList out of VALUE_STRING token
at [Source: (PushbackInputStream); line: 1, column: 335] (through reference chain: com.crmsc.ecc.api.oauth2.domain.feign.product.SpuSyncReqVo["product"]->com.crmsc.ecc.api.oauth2.domain.feign.product.Product["thirdSkus"])"

thirdSkus数组里面有一个数组也需要json 这个可以成功
但是thirdSkus不成功 报错。
https://img2020.cnblogs.com/blog/1084044/202009/1084044-20200903171846578-1612164427.png

锦瑟_华年的主页 锦瑟_华年 | 初学一级 | 园豆:105
提问于:2020-09-03 17:19
< >
分享
最佳答案
0
  • 你现在的 thirdSkus 是一个 string 类型,所以不能成功 转换成 数组

办法:

  1. 把 thirdSkus 先转换成对象
  2. 再转换成 json
$data['thirdSkus'] = json_decode($data['thirdSkus']);
json_encode($data); 
收获园豆:50
Cheney.Zhong.ZCY | 菜鸟二级 |园豆:254 | 2020-09-08 16:58

$data['thirdSkus'] 不是一个json 不能json_decode
报错:json_decode() expects parameter 1 to be string, array given

锦瑟_华年 | 园豆:105 (初学一级) | 2020-09-08 17:19

需要这样的格式:
"product":
"brandId":"1",
"categoryId":"1",
"introduction":"1111",
"productArea":"",
"spuName":"1111",
"thirdSkus":[{
"height":"",
"isEnergySav":"",
"isEnvironmental":"",
"length":"",
"marketPrice":"100",
"modelCode":"",
"otherImage":"https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg",
"packinglist":"",
"prdModel":"",
"primaryImage":"https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg",
"saleAttributes":[{
"aName":"纸箱容量",
"aid":"111",
"desc":"",
"vName":"0-5L",
"vid":"111"
}],
"specAttributes":[{
"aName":"颜色",
"aid":"103191",
"desc":"",
"vName":"黑色",
"vid":"121731"
},{
"aName":"噪音分贝",
"aid":"103200",
"desc":"",
"vName":"20-30dB",
"vid":"121676"
},
{
"aName":"最大碎纸幅面",
"aid":"103202",
"desc":"",
"vName":"A3",
"vid":"121613"
},
{
"aName":"是否是进口产品",
"aid":"103207",
"desc":"",
"vName":"否",
"vid":"121622"
},
{
"aName":"保密等级",
"aid":"103193",
"desc":"",
"vName":"1级",
"vid":"121660"
},
{
"aName":"安全触停",
"aid":"103192",
"desc":"",
"vName":"有",
"vid":"121722"
},{
"aName":"可碎介质",
"aid":"103195",
"desc":"",
"vName":"纸",
"vid":"121659"
},{
"aName":"单次碎纸张数",
"aid":"103194",
"desc":"100张"
},{
"aName":"碎纸速度",
"aid":"103197",
"desc":"",
"vName":"1-2米/分钟",
"vid":"121671"
},{
"aName":"连续碎纸机时间",
"aid":"103196",
"desc":"",
"vName":"0-10分钟",
"vid":"121668"
},{
"aName":"移动轮",
"aid":"103199",
"desc":"",
"vName":"有",
"vid":"121723"
},{
"aName":"碎纸效果",
"aid":"103198",
"desc":"",
"vName":"米粒状",
"vid":"121674"
}],
"saleUnit":"",
"sellPrice":"100",
"skuName":"1111",
"taxRate":"",
"thirdSkuId":"11",
"weight":"",
"weightUnit":"",
"width":""
}],
"thirdSpuId":"1111"
},
"supplierCode":"1111",
"token":"111111111111"
打印出来是这种格式的:
["token"]=> string(32) "111111"
["supplierCode"]=> string(6) "111"
["product"]=> array(6) { ["thirdSpuId"]=> string(5) "111" ["categoryId"]=> string(5) "111" ["spuName"]=> string(37) "111" ["brandId"]=> string(7) "1001924" ["introduction"]=> string(4) "asda" ["thirdSkus"]=> string(536) "{"thirdSkuId":"10077","skuName":"\u6cb3\u5357\u9f0e\u94e7\u79d1\u8d38\u5546\u54c1\u6d4b\u8bd5\u65b0\u589e3","sellPrice":"163.00","marketPrice":195.6,"skuType":1,"primaryImage":"https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg","otherImage":"https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg","saleAttributes":"[{"aid":"10748","vid":"121619","aName":"\u590d\u5370\u7eb8\u89c4\u683c","vName":"B5"}]"}" }

锦瑟_华年 | 园豆:105 (初学一级) | 2020-09-08 17:28

@锦瑟_华年: 把 $data打印下呢?

Cheney.Zhong.ZCY | 园豆:254 (菜鸟二级) | 2020-09-08 17:32

@Cheney.Zhong.ZCY: array(6) {
["thirdSpuId"] => string(5) "10077"
["categoryId"] => string(5) "10174"
["spuName"] => string(37) 11111111113"
["brandId"] => string(7) "1001924"
["introduction"] => string(4) "asda"
["thirdSkus"] => string(536) "{"thirdSkuId":"10077","skuName":"\u6cb3\u5357\u9f0e\u94e7\u79d1\u8d38\u5546\u54c1\u6d4b\u8bd5\u65b0\u589e3","sellPrice":"163.00","marketPrice":195.6,"skuType":1,"primaryImage":"https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg","otherImage":"https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg","saleAttributes":"[{"aid":"10748","vid":"121619","aName":"\u590d\u5370\u7eb8\u89c4\u683c","vName":"B5"}]"}"
}

锦瑟_华年 | 园豆:105 (初学一级) | 2020-09-08 17:34

@锦瑟_华年:
改为直接赋值

Cheney.Zhong.ZCY | 园豆:254 (菜鸟二级) | 2020-09-08 17:47

@Cheney.Zhong.ZCY:
直接赋值之后 这个双引号就不见了
"saleAttributes":[{"aid":"10748","vid":"121619","aName":"\u590d\u5370\u7eb8\u89c4\u683c","vName":"B5"}]}

但是
thirdSkus 是还string类型

锦瑟_华年 | 园豆:105 (初学一级) | 2020-09-08 17:50

@锦瑟_华年:
$data["thirdSkus"] 的结构应该如下:

$data["thirdSkus"] = array(
            'thirdSkuId' => '10077',
            'skuName' => '河南鼎铧科贸商品测试新增3',
            'sellPrice' => '163.00',
            'marketPrice' => 195.6,
            'skuType' => 1,
            'primaryImage' => 'https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg',
            'otherImage' => 'https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg',
            'saleAttributes' => array(array(
                'aid' => '10748',
                'vid' => '121619',
                'aName' => '复印纸规格',
                'vName' => 'B5',
            ))
        );
var_dump(json_encode($data['thirdSkus']));

另外:

aid, vid, thirdSkuId 等,如果 java 定义的不是 string, 还是无法解析。

你需要把 aid, vid 等转成 int, price 转成 float 后再调用 json_encode

Cheney.Zhong.ZCY | 园豆:254 (菜鸟二级) | 2020-09-08 18:00

@Cheney.Zhong.ZCY: 打印出还是string的类型
打印信息如下:
string(513) "{"thirdSkuId":"10077","skuName":"\u6cb3\u5357\u9f0e\u94e7\u79d1\u8d38\u5546\u54c1\u6d4b\u8bd5\u65b0\u589e3","sellPrice":"163.00","marketPrice":195.6,"skuType":1,"primaryImage":"https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg","otherImage":"https://img14.360buyimg.com/n0/jfs/t1/63929/37/643/505682/5cecf5beEbebd72c6/ff6968ffc1256f4f.jpg","saleAttributes":[{"aid":"10748","vid":"121619","aName":"\u590d\u5370\u7eb8\u89c4\u683c","vName":"B5"}]}"
array(4) {
["success"] => bool(false)
["resultCode"] => string(2) "99"
["resultMsg"] => string(479) "JSON parse error: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 92] (through reference chain: com.crmsc.ecc.api.oauth2.domain.feign.product.SpuSyncReqVo["product"]->com.crmsc.ecc.api.oauth2.domain.feign.product.Product["thirdSkus"])"
["result"] => NULL
}

锦瑟_华年 | 园豆:105 (初学一级) | 2020-09-08 18:08

@Cheney.Zhong.ZCY: 你打印出来的是对的吗?是不是我的数组里面的数据类型不对。我打印看一下

锦瑟_华年 | 园豆:105 (初学一级) | 2020-09-08 18:09

已经解决 谢谢你

锦瑟_华年 | 园豆:105 (初学一级) | 2020-09-15 15:16
其他回答(1)
0

加了一个[0]就解决了

锦瑟_华年 | 园豆:105 (初学一级) | 2020-09-15 15:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册