Google Script JSON解析异常:数组显示为[Ljava.lang.Object;@xxx问题求助
解决JSON解析后出现Java对象标识的问题
问题场景
用以下代码解析Web请求响应:
for (var [key,val] of Object.entries(jsonObj)) { console.log("Key="+key); console.log("Value="+val); }
这段代码对某一个URL有效,但对另一个URL返回如下结果:
{members=[Ljava.lang.Object;@3e0dc74, list_id=xxxxx, total_items=2.0, _links=[Ljava.lang.Object;@610c2a56}
响应本身逻辑正常,但经过JSON.parse()处理后出现异常:members列表没有显示预期内容,而是变成[Ljava.lang.Object;这类Java对象标识。已知响应是JSON嵌套数组结构,但不清楚如何处理。
正常的响应示例(标准JSON格式):
{"members":[{"id":"14d4xd529edc51eac6c2","email_address":"xxx@hotmail.com","unique_email_id":"421ccfc0de","contact_id":"56f0eb4b2034f1228b3ea76e5eac22c6","full_name":"www","web_id":522903574,"email_type":"html","status":"subscribed","consents_to_one_to_one_messaging":true,"merge_fields":{"FNAME":"www","LNAME":"eeee","ADDRESS":{"addr1":"22 du plaisir","addr2":"","city":"xxxxxx","state":"qc","zip":"xxxx","country":"US"},"PHONE":"","MMERGE6":"","MMERGE5":"","MMERGE9":"","MMERGE10":"","MMERGE7":"","MMERGE8":""},"interests":{"e539c5b413":true,"8b4b1cc4be":true,"ff755b6893":false,"e78e70c1ec":false,"7bb4f39707":false,"13fd433d6b":false,"d2c372d6d0":false,"8450588744":false,"461896449a":false,"d5514d2d0b":false,"5dfb9c860b":false,"5775586056":false,"d07314aaf0":false,"851b0ab3e5":false,"9323e42080":false,"5c00098a74":false,"19fcaa30f9":false,"534f03a40c":false,"bd85a86aae":false,"a5abdc2e42":false,"e7b4a3ada0":false,"970e155af0":false,"0569ab3c82":false,"09c995ef83":false,"de65267b73":false,"7dd7155626":false,"2adcefc913":false,"f87d68d423":false,"83d27bff8f":false,"934b62ae80":false,"ff41de7dff":false,"d70e507585":false,"08045c1b3d":false,"02a8543521":true,"c2a43803b1":false,"55d1bbb773":false,"27abb86269":false},"stats":{"avg_open_rate":0,"avg_click_rate":0,"ecommerce_data":{"total_revenue":0,"number_of_orders":0,"currency_code":"CAD"}},"ip_signup":"","timestamp_signup":"","ip_opt":"xx4.x2.1x.1xx","timestamp_opt":"2024-01-17T23:51:54+00:00","member_rating":2,"last_changed":"2024-01-31T13:57:04+00:00","language":"","vip":false,"email_client":"iPhone","location":{"latitude":0,"longitude":0,"gmtoff":0,"dstoff":0,"country_code":"","timezone":"","region":""},"source":"Admin Add","tags_count":0,"tags":[],"list_id":"fx3240b7","_links":[{"rel":"self","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"parent","href":"https:","method":"GET","targetSchema":"https:","schema":"https:"},{"rel":"update","href":"https:","method":"PATCH","targetSchema":"https:","schema":"https:"},{"rel":"upsert","href":"https:","method":"PUT","targetSchema":"https:","schema":"https:"},{"rel":"delete","href":"https:","method":"DELETE"},{"rel":"activity","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"goals","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"notes","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"events","href":"https:","method":"POST","targetSchema":"https:"},{"rel":"delete_permanent","href":"https:","method":"POST"}]},{"id":"be87b47fe6cebade61ced3fe638b13dc","email_address":"xxx@ss.COM","unique_email_id":"788b930fc5","contact_id":"0665a6462a9dfa2fd2350bbeecae45ff","full_name":"pat","web_id":522903562,"email_type":"html","status":"subscribed","consents_to_one_to_one_messaging":true,"merge_fields":{"FNAME":"pat","LNAME":"laf","ADDRESS":{"addr1":"20 du saint-\u00e9milion","addr2":"","city":"bcccam","state":"qc","zip":"ccc","country":"CA"},"PHONE":"","MMERGE6":"","MMERGE5":"","MMERGE9":"","MMERGE10":"","MMERGE7":"","MMERGE8":""},"interests":{"e539c5b413":true,"8b4b1cc4be":false,"ff755b6893":false,"e78e70c1ec":false,"7bb4f39707":false,"13fd433d6b":false,"d2c372d6d0":false,"8450588744":false,"461896449a":false,"d5514d2d0b":false,"5dfb9c860b":false,"5775586056":false,"d07314aaf0":false,"851b0ab3e5":false,"9323e42080":false,"5c00098a74":false,"19fcaa30f9":false,"534f03a40c":false,"bd85a86aae":false,"a5abdc2e42":false,"e7b4a3ada0":false,"970e155af0":false,"0569ab3c82":false,"09c995ef83":false,"de65267b73":false,"7dd7155626":false,"2adcefc913":false,"f87d68d423":false,"83d27bff8f":false,"934b62ae80":false,"ff41de7dff":false,"d70e507585":false,"08045c1b3d":false,"02a8543521":false,"c2a43803b1":false,"55d1bbb773":false,"27abb86269":false},"stats":{"avg_open_rate":0,"avg_click_rate":0,"ecommerce_data":{"total_revenue":0,"number_of_orders":0,"currency_code":"CAD"}},"ip_signup":"","timestamp_signup":"","ip_opt":"xx.9x.1xx.1xx","timestamp_opt":"2024-01-17T23:50:35+00:00","member_rating":2,"last_changed":"2024-01-26T16:03:04+00:00","language":"","vip":false,"email_client":"","location":{"latitude":29.46,"longitude":-98.57,"gmtoff":-6,"dstoff":-5,"country_code":"US","timezone":"america/chicago","region":"tx"},"source":"Admin Add","tags_count":0,"tags":[],"list_id":"fcc73240b7","_links":[{"rel":"self","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"parent","href":"https:","method":"GET","targetSchema":"https:","schema":"https:"},{"rel":"update","href":"https:","method":"PATCH","targetSchema":"https:","schema":"https:"},{"rel":"upsert","href":"https:","method":"PUT","targetSchema":"https:","schema":"https:"},{"rel":"delete","href":"https:","method":"DELETE"},{"rel":"activity","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"goals","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"notes","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"events","href":"https:","method":"POST","targetSchema":"https:"},{"rel":"delete_permanent","href":"https:","method":"POST"}]}],"list_id":"fcc73240b7","total_items":2,"_links":[{"rel":"self","href":"https:","method":"GET","targetSchema":"https:","schema":"https:"},{"rel":"parent","href":"https:","method":"GET","targetSchema":"https:"},{"rel":"create","href":"https:","method":"POST","targetSchema":"https:","schema":"https:"}]}
解决思路
1. 定位问题根源
出现[Ljava.lang.Object;@xxx这种标识,说明后端返回的不是标准JSON格式,而是Java对象直接调用toString()方法的输出。这种格式不符合JSON规范,JSON.parse()自然无法正确解析。
2. 优先修复后端输出
这是最彻底的解决方案:
- 要求后端使用Jackson、Gson等Java JSON序列化库,将对象序列化为标准JSON字符串。
- 确保响应的
Content-Type头设置为application/json,让前端明确知道这是JSON数据。
3. 前端临时兼容方案(仅当后端无法修改时)
如果后端暂时无法调整,可以尝试将非标准的响应字符串转换为标准JSON:
function fixNonStandardResponse(rawStr) { // 替换=为:,给键名加引号 let fixedStr = rawStr.replace(/(\w+)=/g, '"$1":'); // 替换Java数组标识为[](简单处理,复杂场景需调整) fixedStr = fixedStr.replace(/\[Ljava\.lang\.Object;@[0-9a-fA-F]+/g, '[]'); // 处理大括号外的多余内容(如果有) fixedStr = fixedStr.replace(/^{(.*)}$/, '{$1}'); return fixedStr; } // 使用示例 const rawResponse = '{members=[Ljava.lang.Object;@3e0dc74, list_id=xxxxx, total_items=2.0, _links=[Ljava.lang.Object;@610c2a56}'; try { const fixedJson = fixNonStandardResponse(rawResponse); const jsonObj = JSON.parse(fixedJson); // 正常解析后处理数据 for (var [key,val] of Object.entries(jsonObj)) { console.log("Key="+key); console.log("Value="+val); } } catch(e) { console.error("转换或解析失败:", e); }
注意:这种临时方案只适用于简单场景,复杂嵌套结构可能会出错,优先推荐修复后端。
4. 优化前端解析逻辑
即使拿到标准JSON,原代码直接打印数组/对象会显示[object Object],可以优化为递归打印:
function printJson(obj, prefix = '') { for (const [key, val] of Object.entries(obj)) { const currentPrefix = prefix ? `${prefix}.${key}` : key; if (typeof val === 'object' && val !== null) { console.log(`${currentPrefix}:`); printJson(val, currentPrefix); } else { console.log(`${currentPrefix}=${val}`); } } } // 使用示例 printJson(jsonObj);
内容的提问来源于stack exchange,提问作者Patpower
相关产品推荐
相关产品推荐

