【问题标题】:React Native JSON Value of type NSDictionary Cannot be converted to NSStringNSDictionary 类型的 React Native JSON 值无法转换为 NSString
【发布时间】:2017-04-11 13:34:30
【问题描述】:

我正在尝试开发的 React Native(版本 0.38.0)应用程序存在问题。我基本上是边走边学。

我正在从自定义 API 端点获取 JSON。我已经设法遍历 JSON 并输出一些数据,但问题在于 string 链接到图像。我不断收到错误:

JSON Value of { robj = "https://example.com/img.jpg" } type NSDictionary Cannot be converted to NSString.

在 google 周围有一个侦察员,我看不出这个问题的头或尾。

谁能指点我正确的方向?

var api = {
  getData() {
    var url = 'https://example.com/api/json/'
    return fetch(url).then((res) => res.json())
  }
}

class GetCategories extends Component {
  constructor(props) {
    super(props)
    this.state = {
      categories: []
    }
  }

  componentWillMount() {
    api.getData().then((res) => {
      this.setState({
        categories: res
      })
    })  
  }

  render() {
    var catLoop =this.state.categories.map(function(obj, index){
      var catTitle = '',
          catImage = '';

      catTitle = obj.title;
      catImage = obj.image;

      return (
        <View key={index}>
          <Image source={{uri: {catImage}}} style={{width: 400, height: 400}}>
            <Text>{catTitle}</Text>
          </Image>
        </View>
      )

    });

    return <View>{catLoop}</View>
  }
}

【问题讨论】:

  • 我通过添加 {catTitle} 解决了这个问题 但图片不会显示。
  • 你确定你包括了撇号吗?问题是您将变量包装在括号内 { } 使其成为一个对象。
  • 删除了撇号。图像现在显示。最后一行代码是: {catTitle} 谢谢你的帮助!
  • 您提交此内容时,我正在起草我的答案。看来你自己想通了。 :D

标签: javascript json react-native


【解决方案1】:

这应该可以解决你的问题...

<Image source={{uri: catImage}} style={{width: 400, height: 400}}>
     <Text>{catTitle}</Text>
</Image>

【讨论】:

  • 事先在cmets中如上解决。还是谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 2013-09-15
相关资源
最近更新 更多