【问题标题】:how to render array data from api?如何从 api 渲染数组数据?
【发布时间】:2019-09-27 09:30:25
【问题描述】:

我想从已经在数组中的api渲染DetectedText,那么如何渲染它或者下面的渲染方法是反应代码

import React, { Component } from 'react';
import axios from 'axios'




class Image extends Component {
    constructor(props) {
        super(props);
        this.state = { file: '', imagePreviewUrl: '', selectedOption: '', change: [], response: [], path: [], jsonData:[] };
    }

    handleSubmit(e) {

        e.preventDefault();


        var byteCode = this.state.imagePreviewUrl.substring((e.target.value).indexOf(',') + 23);

        console.log('base64 byte code substring data', byteCode);


        let url = "http://192.168.4.138/MediaAnalysisImage_Amazon/api/DetectText/DetectText"
            console.log(url);
            const data = {  "fileByte": byteCode }


            console.log(data)

            const response = axios.post(url, data)
                .then(response => {
                    this.setState({
                        change: response,
                        byteArr: response.data.fileByte,
                        jsonData: response.data.jsondata.replace(/[&\/\\#+()$"~%.'*?<>{}]/g, ''),

                        path: response.data.jsondata.Confidence


                    });
                    console.log('Byte data', this.state.byteArr)
                    console.log('json detected data', response.data.jsondata.DetectedText)
                })




    }

    radioChange = (e) => {
        this.setState({
            selectedOption: e.target.value

        });

    }
    handleImageChange(e) {
        e.preventDefault();

        let reader = new FileReader();
        let file = e.target.files[0];


        reader.onloadend = () => {
            this.setState({
                file: file,
                imagePreviewUrl: reader.result



            });

        }

        reader.readAsDataURL(file)


    }

    render() {
        const img = "data:image/png;base64" + ',' + this.state.byteArr
        let { imagePreviewUrl } = this.state;

        let $imagePreview = null;
        if (imagePreviewUrl) {
            $imagePreview = (<img src={imagePreviewUrl} className="img-responsive imgp" />);
            console.log(imagePreviewUrl)
        } else {
            $imagePreview = (<span className="previewText">Please select an Image for Preview</span>);
        }

        return (
            <div className="wrapper">
                <h2 className="text-center heading" >Text Recognization</h2>
                <div className="container ">

                    <section className="celeb">
                        <form className="Fform bg-light mb-4">

                            <div class="form-check">
                                <label class="form-check-label">
                                    <input type="radio" class="form-check-input" name="optradio" value="face" onChange={this.radioChange} />Face
                         </label>
                            </div>
                            <div class="form-check">
                                <label class="form-check-label">
                                    <input type="radio" class="form-check-input" name="optradio" value="celeb" onChange={this.radioChange} />Celeb
                         </label>
                            </div>
                            <div class="form-check mb-4">
                                <label class="form-check-label">
                                    <input type="radio" class="form-check-input" name="optradio" value="text" onChange={this.radioChange} />Text
                         </label>
                            </div>
                            <div class="form-group ">
                                <input className="fileInput"
                                    type="file"
                                    onChange={(e) => this.handleImageChange(e)} class="btn btn-secondary" />
                            </div>
                            <button className="btn btn-success"
                                type="submit"
                                onClick={(e) => this.handleSubmit(e)}>Upload Image</button>
                        </form>
                        <hr></hr>
                        <div className="row grid">
                            <div className="col-md-6">
                                <h3>Input Image</h3>
                                {$imagePreview}
                            </div>
                            <div className="col-md-6">
                                <h3>Output Image</h3>



                                <img src={img} className="img-responsive imgp" />
                            </div>
                        </div>
                        <div>
                            <hr></hr>

                            <h4>Description </h4>
                            <table className="table table-hover">
                                <tr>
                                    <th ><label>Name :- </label></th>
                                    <td>{this.state.jsonData}</td>


                                </tr>
                            </table>


                        </div>
                    </section>
                </div>
            </div>
        )
    }
}
export default Image;


下面是api返回数据和添加api返回数据的图像,所以请检查并告诉我应该做些什么改变

api return data 只需查看标记的文本,即 DetectedText 并告诉我如何呈现它

【问题讨论】:

  • 你能提供你得到的数据吗?
  • 从邮递员那里复制数据,然后把它放到你的 github 链接中。你给出的格式不好的数据集有很多错误。
  • 请在添加邮递员数据时再次检查链接

标签: javascript arrays reactjs


【解决方案1】:

首先使用JSON.parse它会转换你的数据哟数组对象然后循环遍历你的所有数据并做剩下的事情

var data = {
    "filename": null,
    "fileByte": "", // ur response data 
    "jsondata": "" // ur response data
}

var parseData = JSON.parse(data.jsondata)
console.log(parseData)
var DetectedText = []
for (x of parseData) {
  console.log(x) // all the data
  console.log(x.Confidence) // single one
  console.log(x.DetectedText)
  DetectedText.push(x.DetectedText)
}
console.log(DetectedText)
this.setState({dText:DetectedText })

然后循环你的 DetectedText 在rende里面 希望对您有所帮助,如有需要请告知

【讨论】:

  • 但显示 eroor “未处理的拒绝 (SyntaxError): 位置 0 处 JSON 中的意外标记 u”
  • 在“console.log('json检测数据', response.data.jsondata.DetectedText)”下面
  • 你解析了哪些数据? response.data.jsondata这个?它不会解析。删除替换代码并尝试plz
  • 是的,首先打印响应,看看它提供了什么数据,如果它与你在 git 中给我的数据相同,然后解析它并完成剩下的工作
  • 我将数据输入控制台并在控制台中显示整个 json 数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 2021-01-18
相关资源
最近更新 更多