【问题标题】:How to parse child item in react native?如何在本机反应中解析子项?
【发布时间】:2021-11-21 21:08:01
【问题描述】:
<View style={{ flexDirection: 'row' }}>
<AntDesign name="flag" size={12} color="black" style={{ marginTop: 3, marginRight: 10 }} />
<Text style={styles.H1}>{item.name}</Text>
 <Text style={styles.H2}>{item.location}</Text>
 </View>

item 正在正确解析 json 名称。但我在 {flag:'', country:''}

位置有两个项目

如何在上面的代码中解析这两项。

【问题讨论】:

    标签: reactjs json native


    【解决方案1】:

    如果您正在尝试渲染位置,那么您可以尝试这个提供的位置是一个带有标志和国家作为位置属性的对象

    <View style={{ flexDirection: 'row' }}>
          <AntDesign name="flag" size={12} color="black" style={{ marginTop: 
         3, marginRight: 10 }} />
         <Text style={styles.H1}>{item.name}</Text>
         <Text style={styles.H2}>{item.location.country} </Text>
         <Text style={styles.H2}>{item.location.flag} </Text>
    </View>
    

    【讨论】:

    • 我试过了,显示如下错误:Cannot read properties of undefined (reading 'country') The json array is: { "name": "Judy Dixon", "location": { “国家”:“也门”,“国旗”:“”}}
    • 如果是正确的,如果位置部分被删除,应用程序会显示 Judy Dixon?如果这是真的,那么某些东西正在改变位置对象,这就是为什么我们不能在其上调用 ._country.country ,它不应该抛出错误,请检查你是否显示项目列表可能有些项目没有位置对象
    • 它是位置,默认值为“也门”。名称显示正确。如果添加国家代码,则会引发错误。
    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2019-03-12
    • 1970-01-01
    • 2022-09-27
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多