繁体   English   中英

解析我的 json 以获取值 - 从 Xcode 发送到 JS 前端的 json

[英]parse my json to get values - json sent from Xcode to JS front-end

试图解析和读取我的 JSON 数据。
我正在将我的 json 数据从 Xcode 传递到我的 React 本机前端。
我试过 JSON.parse 和 JSON.stringify 没有任何效果。
它总是记录“NULL”。 我需要访问“价值”帮助!

JSON 在 Xcode

{
  "myDictionary" : {
    "BG" : [
      "{\"value\":\"8 mg\\\/dL\",\"endDate\":635390040,\"startDate\":635390040,\"type\":\"bloodGlucose\"}",
      "{\"value\":\"6 mg\\\/dL\",\"endDate\":635393640,\"startDate\":635393640,\"type\":\"bloodGlucose\"}"
    ]
  }
}

JS:

const log = HealthkitController.getBloodGlucose()
    .then(result => {
     let res = JSON.parse(result)
      for (let i = 0; i < res.length; i++) {
        let final = res[0];        
        console.log(final)         // prints the first object
        let fin = final["value"]
        console.log(fin)           //undefined (doesn't print 8mg/dL)
 }
})

结果:

["{\"value\":\"8 mg\\\/dL\",\"endDate\":635390040,\"startDate\":635390040,\"type\":\"bloodGlucose\"}",
"{\"value\":\"6 mg\\\/dL\",\"endDate\":635393640,\"startDate\":635393640,\"type\":\"bloodGlucose\"}"]

这创造了什么?

 const log = HealthkitController.getBloodGlucose().then(result => { for (var i = 0; i < result.lenght; i++{ let res = JSON.parse(result[i]) console.log(res) //always null } });

您的结果以字符串数组的形式返回。 如果你想提取价值,你可以做这样的事情

const values = results.map(result => {
 const { value } = JSON.parse(result);
 return value;
});

output 将是:

["8 mg/dL", "6 mg/dL"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM