【问题标题】:Getting the path of an image stored locally from a JSON file - React Native从 JSON 文件获取本地存储的图像的路径 - React Native
【发布时间】:2021-06-13 21:36:07
【问题描述】:

我一直在寻找一种方法来从 JSON 文件 中获取本地存储在我的项目中的图像路径。 我已经看到建议删除 JSON 文件 并在 .JS 文件 中使用数组的答案,就像这样:

const DATA = [
   {
    "ID": 0,
    "Image": require('../assets/image0.png')
   },
   {
    "ID": 1,  
    "Image": require('../assets/image1.png')
   }
]

问题是我只能使用 JSON 文件 来获取我的数据,这是将 Excel 转换为 JSON 文件 的结果,并且没有其他的。所以假设我有一个 data.json 文件如下:

{  
  "data": [
        {
            "ID": 0,
            "Image": "require('../assets/image0.png')"
        },
        {
            "ID": 1,  
            "Image": "require('../assets/image1.png')"
        }
    ]
}

目前,无法使用存储在“图像”属性中的路径作为<Image source = {}/> 中的源来显示图像。有人知道如何保存 JSON 文件并以某种方式获取和使用其中的路径吗?

编辑:

我的结构:

- assets
       - favicon.png // Image I want to display
       - icon.png // Image I want to display
- screens
       - File1.js // Where I want to display images
       - images.json // From where to get paths

文件1.js

 // Other imports
 import assets from "./images.json;
 require("../assets/favicon.png");
 require("../assets/icon.png");

const File1 = () => {
    return (
       <View>
        {assets.data.map((i) => (
            <Image
              source={{
                uri: require(i.Image.replace("require('", "").replace("')", ""))
              }}
              resizeMode="contain"
              style={styles.logo}
            />
          ))}
            {/*<Image source={require('../assets/favicon.png')}></Image> Its working but not above*/}
      </View>
    )
};

export default File1;

const styles = StyleSheet.create({
   logo: {
        height: 80
    },
});

images.json

{
    "data": [
        {
            "ID": 0,
            "Image": "require('../assets/favicon.png')"
        },
        {
            "ID": 1,
            "Image": "require('../assets/icon.png')"
        }
    ]
}

但是得到:

TransformError src\screens\File1.js: src\screens\File1.js:Invalid call at line 323: require(i.Image.replace("require('", "").replace("')", ""))
- node_modules\react-native\Libraries\Utilities\HMRClient.js:320:31 in showCompileError
- node_modules\react-native\Libraries\Utilities\HMRClient.js:227:26 in client.on$argument_1
- node_modules\eventemitter3\index.js:181:21 in emit
- node_modules\metro\src\lib\bundle-modules\HMRClient.js:142:10 in _ws.onmessage
- node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\WebSocket\WebSocket.js:231:8 in _eventEmitter.addListener$argument_1
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue

【问题讨论】:

    标签: javascript arrays json react-native


    【解决方案1】:

    您只需将路径存储在数组中,例如:

    const images = {data : [
        {
            "ID": 0,
            "Image": "require('../assets/image0.png')"
        },
        {
            "ID": 1,  
            "Image": "require('../assets/image1.png')"
        }
    ]}
    

    在你的渲染组件中:

    return (<>{
    images.data.map(i => <Image source={require(i.Image.replace("require('", "").replace("')", ""))}/>)
    }</>)
    

    【讨论】:

    • 它不仅不起作用,而且还需要我创建一个对象数组并手动指定我不想要的路径。我想保留我的 JSON 并从中获取路径。
    • 您的 json 格式不正确
    • 修正错字。现在想看看这个问题吗?
    • 我编辑了答案。您可以从字符串中删除require人员以获取路径并在图像中使用require
    • 在 require 中仍然收到无效调用。
    猜你喜欢
    • 2020-10-24
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2018-10-20
    • 1970-01-01
    • 2018-04-24
    相关资源
    最近更新 更多