简体   繁体   English

如何在反应中从嵌套的 json 中提取数据

[英]How to extract data from nested json in react

I have madethe following code.我已经制作了以下代码。 So I have to retrieve data from json and present them in react.所以我必须从 json 中检索数据并在反应中呈现它们。 I have basicly one nested .joson object inside the other one.我基本上有一个嵌套的 .joson 对象在另一个里面。 I know to to retrieve needed data from "root" json, but have a problem with doing it for other one.我知道要从“根”json 中检索所需的数据,但是在为其他数据执行此操作时遇到问题。 In this example I got as output在这个例子中,我得到了输出在此处输入图片说明 Now I don't need all thath json object inside, just Neko from name.现在我不需要里面的所有 json 对象,只需要名字中的 Neko。 I have tried {props.pred.nastavnik.name} but it did not work.我试过 {props.pred.nastavnik.name} 但没有用。 (it didn't gave any output) (它没有给出任何输出)

 <CardContent>
            <Typography className={classes.title} color="textSecondary" gutterBottom>
                Nastavnik
            </Typography>
            <Typography variant="h5" component="h2">
                {props.pred.nastavnik}
            </Typography>
            <Typography className={classes.title} color="textSecondary" gutterBottom>
                Br studenata
            </Typography>
            <Typography variant="h5" component="h2">
                {props.pred.broj_studenata}
            </Typography>



        </CardContent>

Every help would be great每一个帮助都会很棒

nastavnik variable is a string. nastavnik 变量是一个字符串。 You should convert it to object by: JSON.parse(str) For example:您应该通过以下方式将其转换为对象: JSON.parse(str) 例如:

 const str = '{"id":2,"admin":false,"name":"Neko","sifra":"sifra"}' console.log(JSON.parse(str))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM