【问题标题】:Get the value from the JSON file using React Native使用 React Native 从 JSON 文件中获取值
【发布时间】:2018-01-26 18:04:17
【问题描述】:

我在 DocumentDirectoryPath 中有我的 JSON 文件,JSON 文件看起来像

{
    "name": {
        "en_EN": "Epic App Dev Self"
    },
    "description": {
        "en_EN": ""
    },
    "type": "course",
    "id": "epicdevself",
    "value": {
        "version": "1.0.1",
        "series_prefix": "eds"
    },
    "uid": "epicdevself/1.0.1",
    "children": [{
        "name": {
            "en_EN": "Level 1"
        },
        "description": {
            "en_EN": ""
        },
        "type": "level",
        "id": "level1",
        "value": {
            "cover": {
                "targetLanguage": ""
            }
        },
        "uid": "epicdevself/1.0.1/level1",
        "children": [{
            "name": {
                "en_EN": "Unit 1"
            },
            "description": {
                "en_EN": ""
            },
            "type": "unit",
            "id": "unit1",
            "value": {
                "cover": {
                    "teacher": "https://origin-dashboard.com/CDN/ProdStore/EPIC_APP/packets.zip",
                    "targetLanguage": ""
                }
            },
            "uid": "epicdevself/1.0.1/level1/unit1",
            "children": [{
                    "name": {
                        "en_EN": "Lesson 1"
                    },
                    "description": {
                        "en_EN": ""
                    },
                    "type": "lesson",
                    "id": "lesson1",
                    "value": {
                        "targetLanguage": ""
                    },
                    "uid": "epicdevself/1.0.1/level1/unit1/lesson1",
                    "children": [{
                            "name": {
                                "en_EN": "Listening and speaking"
                            },
                            "description": {
                                "en_EN": ""
                            },
                            "type": "resource_group",
                            "id": "skill2",
                            "value": {
                                "display_mode": "icons"
                            },
                            "uid": "epicdevself/1.0.1/level1/unit1/lesson1/skill2",
                            "children": [{
                                "name": {
                                    "en_EN": "Listening activity"
                                },
                                "description": {
                                    "en_EN": ""
                                },
                                "type": "activity",
                                "id": "activity3",
                                "value": {
                                    "header": "",
                                    "learningObject": null,
                                    "iconUrl": "",
                                    "teachingNotes": "",
                                    "tincanIri": "",
                                    "activityRubric": "",
                                    "audioScript": "",
                                    "activityUri": "",
                                    "pageBook": "",
                                    "time": 0
                                },
                                "uid": "epicdevself/1.0.1/level1/unit1/lesson1/skill2/activity3",
                                "children": [

                                ]
                            }]
                        },
                        {
                            "name": {
                                "en_EN": "Conversation"
                            },
                            "description": {
                                "en_EN": ""
                            },
                            "type": "resource_group",
                            "id": "skill3",
                            "value": {
                                "display_mode": "icons"
                            },
                            "uid": "epicdevself/1.0.1/level1/unit1/lesson1/skill3",
                            "children": [{
                                "name": {
                                    "en_EN": "Conversation Activity"
                                },
                                "description": {
                                    "en_EN": ""
                                },
                                "type": "activity",
                                "id": "activity4",
                                "value": {
                                    "header": "",
                                    "learningObject": null,
                                    "iconUrl": "",
                                    "teachingNotes": "",
                                    "tincanIri": "",
                                    "activityRubric": "",
                                    "audioScript": "",
                                    "activityUri": "",
                                    "pageBook": "",
                                    "time": 0
                                },
                                "uid": "epicdevself/1.0.1/level1/unit1/lesson1/skill3/activity4",
                                "children": [

                                ]
                            }]
                        },
                        {
                            "name": {
                                "en_EN": "Vocabulary"
                            },
                            "description": {
                                "en_EN": ""
                            },
                            "type": "resource_group",
                            "id": "skill1",
                            "value": {
                                "display_mode": "icons"
                            },
                            "uid": "epicdevself/1.0.1/level1/unit1/lesson1/skill1",
                            "children": [{
                                    "name": {
                                        "en_EN": "Vocabulary Activity A"
                                    },
                                    "description": {
                                        "en_EN": ""
                                    },
                                    "type": "activity",
                                    "id": "activity1",
                                    "value": {
                                        "header": "",
                                        "learningObject": {
                                            "uri": "",
                                            "source": null
                                        },
                                        "iconUrl": "",
                                        "teachingNotes": "",
                                        "tincanIri": "",
                                        "activityRubric": "",
                                        "audioScript": "",
                                        "activityUri": "",
                                        "pageBook": "",
                                        "time": 0
                                    },
                                    "uid": "epicdevself/1.0.1/level1/unit1/lesson1/skill1/activity1",
                                    "children": [

                                    ]
                                },
                                {
                                    "name": {
                                        "en_EN": "Vocabulary Activity B"
                                    },
                                    "description": {
                                        "en_EN": ""
                                    },
                                    "type": "activity",
                                    "id": "activity2",
                                    "value": {
                                        "header": "",
                                        "learningObject": {
                                            "uri": "",
                                            "source": null
                                        },
                                        "iconUrl": "",
                                        "teachingNotes": "",
                                        "tincanIri": "",
                                        "activityRubric": "",
                                        "audioScript": "",
                                        "activityUri": "",
                                        "pageBook": "",
                                        "time": 0
                                    },
                                    "uid": "epicdevself/1.0.1/level1/unit1/lesson1/skill1/activity2",
                                    "children": [

                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": {
                        "en_EN": "Lesson 2"
                    },
                    "description": {
                        "en_EN": ""
                    },
                    "type": "lesson",
                    "id": "lesson2",
                    "value": {
                        "teacher": "https://origin-dashboard.com/CDN/ProdStore/EPIC_APP/packets.zip",
                        "targetLanguage": ""
                    },
                    "uid": "epicdevself/1.0.1/level1/unit1/lesson2",
                    "children": [

                    ]
                },
                {
                    "name": {
                        "en_EN": "Lesson 3"
                    },
                    "description": {
                        "en_EN": ""
                    },
                    "type": "lesson",
                    "id": "lesson3",
                    "value": {
                        "teacher": "https://origin-dashboard.com/CDN/ProdStore/EPIC_APP/packets.zip",
                        "targetLanguage": ""
                    },
                    "uid": "epicdevself/1.0.1/level1/unit1/lesson3",
                    "children": [

                    ]
                },
                {
                    "name": {
                        "en_EN": "Audio"
                    },
                    "description": {
                        "en_EN": ""
                    },
                    "type": "lesson",
                    "id": "audio1",
                    "value": {
                        "targetLanguage": ""
                    },
                    "uid": "epicdevself/1.0.1/level1/unit1/audio1",
                    "children": [

                    ]
                },
                {
                    "name": {
                        "en_EN": "Video"
                    },
                    "description": {
                        "en_EN": ""
                    },
                    "type": "lesson",
                    "id": "video1",
                    "value": {
                        "targetLanguage": ""
                    },
                    "uid": "epicdevself/1.0.1/level1/unit1/video1",
                    "children": [

                    ]
                }
            ]
        }]
    }]
}

从这里我需要打印密钥https://origin-dashboard.com/CDN/ProdStore/EPIC_APP/packets.zip,如何从上面的JSON中获取这个密钥

【问题讨论】:

  • 你能否展示解释如何尝试访问此 JSON 的代码
  • 你不需要 React Native 来做到这一点。 Pure JS 已经拥有你需要的一切。
  • 我正在使用 react native 开发一个混合应用程序我不知道如何在 react 中做到这一点

标签: json reactjs react-native


【解决方案1】:

你需要先导入file.json,你可以这样试试:

import React, { Component } from 'react';
import { View,Text} from 'react-native';
import data  from './file.json';

export default class App extends React.Component {

  render(){
    return (
      <View>
        <Text>
          {data.children[0].children[0].value.cover.teacher}
        </Text>
      </View>
    );
  }
}

希望这个回答对你有帮助。

【讨论】:

  • 我的 JSON 文件路径是 : var path = RNFS.DocumentDirectoryPath + '/test.json' 如何导入这个文件?
  • 你在 react-native-fs 文档上设置好配置了吗?
  • 来自 github.com/itinance/react-native-fs#basic ,我认为您需要使用 readDir(RNFS.DocumentDirectoryPath) 方法并返回 promise 从目录中读取文件。很抱歉帮不了你,因为我从来没有用过这个库,所以我需要先了解一下。
  • 如何使用循环执行它来打印JSON文件中呈现的所有键值?
猜你喜欢
  • 2020-07-10
  • 1970-01-01
  • 2019-01-07
  • 2018-10-27
  • 2019-12-15
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 2021-11-10
相关资源
最近更新 更多