简体   繁体   中英

JSON Response Parsing Error - Javascript

I have the following JSON response coming from an API.

{
    "status": true,
    "cakes": {
        "7689": {
            "id": 7689,
            "flavor": "chocolate",
            "cookDetails": {
                "id": 101,
                "firstName": "Name1",
                "lastName": "LastName1"
            }
        },
        "7690": {
            "id": 7690,
            "flavor": "vanilla",
            "cookDetails": {
                "id": 102,
                "firstName": "Name2",
                "lastName": "LastName2"
            }
        }
    }
}

Language I'm using to parse this JSON: Javascript

Framework: ReactNative

My question is:

1. Is the JSON format correct?

2. If yes, then how do I parse it (NOTE: I don't know the value of id in cakes until I parse it)?

PS: New to the framework. Big thanks.

Try using this,

{
    "status": true,
    "cakes": [{
        "id": 7689,
        "flavor": "chocolate",
        "cookDetails": {
            "id": 101,
            "firstName": "Name1",
            "lastName": "LastName1"
        }
    }, {
        "id": 7690,
        "flavor": "vanilla",
        "cookDetails": {
            "id": 102,
            "firstName": "Name2",
            "lastName": "LastName2"
        }
    }]
}

for ReactNative check this : https://facebook.github.io/react-native/docs/network.html http://www.9lessons.info/2017/04/react-native-json-parsing-and-helper.html


Note below code HTML JavaScript for your understanding.


var obj = JSON.parse('{"status": true,"cakes": [{"id": 7689,"flavor": "chocolate","cookDetails": {"id": 101,"firstName": "Name1","lastName": "LastName1"}},{"id": 7690,"flavor": "vanilla","cookDetails": {"id": 102,"firstName": "Name2","lastName": "LastName2"}}]}');

 <!DOCTYPE html> <html> <body> <h2>Create Object from JSON String</h2> <p id="demo"></p> <script> var obj = JSON.parse('{"status": true,"cakes": [{"id": 7689,"flavor": "chocolate","cookDetails": {"id": 101,"firstName": "Name1","lastName": "LastName1"}},{"id": 7690,"flavor": "vanilla","cookDetails": {"id": 102,"firstName": "Name2","lastName": "LastName2"}}]}'); document.getElementById("demo").innerHTML = obj.cakes[0].id +", "+ obj.cakes[0].flavor+", "+obj.cakes[0].cookDetails.id+", "+obj.cakes[0].cookDetails.firstName+", "+obj.cakes[0].cookDetails.lastName; </script> </body> </html> 

Here is valid JSON(just extra commas were removed):

{
    "status": true,
    "cakes": {
        "7689": {
            "id": 7689,
            "flavor": "chocolate",
            "cookDetails": {
                "id": 101,
                "firstName": "Name1",
                "lastName": "LastName1"
            }
        },
        "7690": {
            "id": 7690,
            "flavor": "vanilla",
            "cookDetails": {
                "id": 102,
                "firstName": "Name2",
                "lastName": "LastName2"
            }
        }
    }
}

You can parse it with plain JSON.parse call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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