首页 > 解决方案 > 解析我的 json 以获取值 - 从 Xcode 发送到 JS 前端的 json

问题描述

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

Xcode 中的 JSON

{
  "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\"}"]

标签: javascriptreact-native

解决方案


这创造了什么?

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
            }
        });


推荐阅读