2020-11-26 Unexpected token o in JSON at position 1

做Salesforce之后最常遇到的一个问题就是你需要写写Apex的后台代码,也要学习一点 Aura 和 LWC(Lightning Web Component)的技能,所以经常会遇到解析的问题,如果你对数据解析不正确则会遇到下面的问题:Unexpected token o in JSON at position 1
下面将这个问题整理如下:

  1. 后台返回的是一个Object,例如该方法返回的是一个我们在Apex里面自定义的Wrapper,那么在前端解析的时候我们就要用如下的方法;
@AuraEnabled
public static MatchAccountData isDuplicateCompany(String leadId){}

直接用result去取在wrapper里面定义的值

  isDuplicateCompany({
     leadId: this.recordId
        }).then(result =>{
            if(result.isDuplicate){
                this.handleRecordClick(result.accountId); 
            }
        });
  1. 如果返回值是一个Map 类型的,如下图所示:
    @AuraEnabled()
    public static Map<String, B1X_Button_Visibility_Matrix__mdt> getButtonVisibilityAccess(String stage, String type){}
 assessmentButtonVisibilty({
  stage: this.ass essmentStatus,
  type: this.assessmentType
            })
     .then(result => {
       console.log('result: ', result['Add Additional Roles']);
        if (result['Add Additional Roles'] !== undefined) {
        this.isAddAdditionalRoleVisible = result['Add Additional Roles'].Visible__c;
                    }
       this.handleRiskRoleDfnMdt();
                })
      .catch((error) => {
       this.message = 'Error received: code' + error.errorCode + ', ' +
      'message ' + error.body.message;
     });
        }
  1. 如果你想在console.log里看到后台返回的值,你需要做一下解析:
    JSON.stringify(result)
    JSON.parse() 方法不能用来解析已经解析过的数据,这里的参数是一个JSON string,如果是其他类型则会报错。

可以参考这个链接:
https://thisinterestsme.com/fix-unexpected-token-o-in-json/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容