繁体   English   中英

尽管有可变键名和冒号字符,但仍映射嵌套的 JSON

[英]Mapping nested JSON despite variable key name and colon character

我正在尝试访问 JSON 数据中的嵌套层,例如“标题”。 但是,第一个属性是可变的,其中的:字符也无济于事

"ISBN:nnnnnnnnnnnn"

使用以下方法将值映射到要解构的组件:

renderBooks()
    {
        return this.state.books.map((book, index) =>
            <BookDetail key={index} book={book} />);
    }

    render()
    {
        console.log(this.state);
        return (
            <ScrollView style={{ flex: 1 }}>
                {this.renderBooks()}
            </ScrollView>
        );
    }

成分:

const BookDetail = ({ book }) =>
{
    const { title, author,... } = book;

return (
        <Card>
            <CardSection>
                <View style={styles.headerContentStyle}>
                    <Text style={headerTextStyle}>{title}</Text>
                    <Text>{author}</Text>
                </View>
            </CardSection>
        </Card>
    );
};

JSON:

[
    {
        "ISBN:9780192853462": {
            "publishers": [
                {
                    "name": "Oxford University Press, USA"
                }
            ],
            "identifiers": {
                "isbn_13": [
                    "9780192853462"
                ],
                "openlibrary": [
                    "OL7384519M"
                ],
                "isbn_10": [
                    "0192853465"
                ],
                "librarything": [
                    "49548"
                ],
                "goodreads": [
                    "334271"
                ]
            },
            "subtitle": "A Very Short Introduction (Very Short Introductions)",
            "title": "Social and Cultural Anthropology",
            "url": "https://openlibrary.org/books/OL7384519M/Social_and_Cultural_Anthropology",
            "number_of_pages": 168,
            "cover": {
                "small": "https://covers.openlibrary.org/b/id/119856-S.jpg",
                "large": "https://covers.openlibrary.org/b/id/119856-L.jpg",
                "medium": "https://covers.openlibrary.org/b/id/119856-M.jpg"
            },
            "subjects": [
                {
                    "url": "https://openlibrary.org/subjects/ethnology",
                    "name": "Ethnology"
                }
            ],
            "publish_date": "April 7, 2000",
            "key": "/books/OL7384519M",
            "authors": [
                {
                    "url": "https://openlibrary.org/authors/OL656666A/John_Monaghan",
                    "name": "John Monaghan"
                },
                {
                    "url": "https://openlibrary.org/authors/OL2662612A/Peter_Just",
                    "name": "Peter Just"
                }
            ],
            "ebooks": [
                {
                    "formats": {},
                    "preview_url": "https://archive.org/details/socialculturalan00mona",
                    "availability": "restricted"
                }
            ]
        }
    }

您可以制作自己的book对象副本,所有书籍信息都包含在ISBN属性中,我个人会将其设为 book 对象内的一个简单字段。

 let data = { "ISBN:9780192853462": { "publishers": [ { "name": "Oxford University Press, USA" } ], "identifiers": { "isbn_13": [ "9780192853462" ], "openlibrary": [ "OL7384519M" ], "isbn_10": [ "0192853465" ], "librarything": [ "49548" ], "goodreads": [ "334271" ] }, "subtitle": "A Very Short Introduction (Very Short Introductions)", "title": "Social and Cultural Anthropology", "url": "https://openlibrary.org/books/OL7384519M/Social_and_Cultural_Anthropology", "number_of_pages": 168, "cover": { "small": "https://covers.openlibrary.org/b/id/119856-S.jpg", "large": "https://covers.openlibrary.org/b/id/119856-L.jpg", "medium": "https://covers.openlibrary.org/b/id/119856-M.jpg" }, "subjects": [ { "url": "https://openlibrary.org/subjects/ethnology", "name": "Ethnology" } ], "publish_date": "April 7, 2000", "key": "/books/OL7384519M", "authors": [ { "url": "https://openlibrary.org/authors/OL656666A/John_Monaghan", "name": "John Monaghan" }, { "url": "https://openlibrary.org/authors/OL2662612A/Peter_Just", "name": "Peter Just" } ], "ebooks": [ { "formats": {}, "preview_url": "https://archive.org/details/socialculturalan00mona", "availability": "restricted" } ] } }; //let isbn = Object.keys(obj).find(item => item.match(/ISBN:\\d/)); const destructureBook = (obj) => { let book = {}; Object.keys(obj).map(function(key) { if(key.match(/ISBN:\\d/)) book['isbn'] = key; Object.keys(obj[key]).map(field => book[field] = obj[key][field]); }); return book; } // destructure the return of the function const {isbn, title, publish_date } = destructureBook(data); console.log(isbn, title, publish_date)

暂无
暂无

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

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