从对象角度获取价值

时间:2019-06-13 10:01:01

标签: json angular

我想从下面的JSON(resReturn.result.gamingdata.original.success)中提取值

只是想知道为什么我只有做几次字符串化和解析才能得到值。

有人可以告诉我如何简化我的代码吗?

JSON:

{
    "status":"Success",
    "message":"100",
    "resReturn":
    {
        "result":{
            "gamingdata":
            {
                    "headers":{},
                    "original":{"success":"Gaming Data Excel - upload success"},
                    "exception":null
            }
    }
}

}

我的代码:

          let resReturnJSON = JSON.stringify(this.UploadstatusGamingDataExcel.resReturn);
          let resultobj = JSON.parse(resReturnJSON || '{}').result;
          let resultJSON = JSON.stringify(resultobj);
          let gamingdataobj = JSON.parse(resultJSON || '{}').gamingdata;
          let gamingdataJSON = JSON.stringify(gamingdataobj);
          let originalObj = JSON.parse(gamingdataJSON || '{}').original;
          let originalJSON = JSON.stringify(originalObj);
          let successObj = JSON.parse(originalJSON || '{}').success;
          console.log(successObj);

3 个答案:

答案 0 :(得分:1)

检查每个属性的truthiness,直到您点击success属性,然后返回找到的属性或返回空字符串。

const data = {
  "status": "Success",
  "message": "100",
  "resReturn": {
    "result": {
      "gamingdata": {
        "headers": {},
        "original": {
          "success": "Gaming Data Excel - upload success"
        },
        "exception": null
      }
    }
  }
};

const success = (data.resReturn &&
    data.resReturn.result &&
    data.resReturn.result.gamingdata &&
    data.resReturn.result.gamingdata.original.success) ?
    data.resReturn.result.gamingdata.original.success : '';

console.log(success);

答案 1 :(得分:1)

const value = {
    "status": "Success",
    "message": "100",
    "resReturn":
    {
        "result": {
            "gamingdata":
            {
                "headers": {},
                "original": { "success": "Gaming Data Excel - upload success" },
                "exception": null
            }
        }
    }
}
const jsonValue = JSON.stringify(value);
const valueFromJson = JSON.parse(jsonValue);
const success = (((((valueFromJson || {}).resReturn || {}).result || {}).gamingdata || {}).original || {}).success;

答案 2 :(得分:1)

如果您想要具有数组和对象的json通用函数,可以使用此功能,

const data = {
      "status": "Success",
      "message": "100",
      "resReturn": {
        "result": {
          "gamingdata": {
            "headers": {},
            "original": {
              "success": "Gaming Data Excel - upload success"
            },
            "exception": null
          }
        }
      }
    };

    const get = (p, o) =>
      p.reduce((xs, x) =>
        (xs && xs[x]) ? xs[x] : null, o)


    console.log(get(['resReturn', 'result', 'gamingdata', 'original', 'success'], data));

我有一个更简单的解决方案:

    let obj: any;
    try {
        if (data.resReturn.result.gamingdata.original.success) {
            obj = data.resReturn.result.gamingdata.original.success
        }      
    } catch(e) {
        obj = null
    }
    console.log(obj);

对于其他不同方式,您也可以参考this answer