首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将JSON数据加载到DynamoDB本地时出错

将JSON数据加载到DynamoDB本地时出错
EN

Stack Overflow用户
提问于 2018-03-09 16:06:56
回答 1查看 359关注 0票数 0

我有可下载的DynamoDB本地(目前正在运行)。我已经阅读了他们的文档,他们的示例代码正在工作。

我使用名称" Users“创建了Users表

这是UsersCreateTable.html

代码语言:javascript
复制
<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />

    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>

    <script>
AWS.config.update({
  region: "us-west-2",
  endpoint: 'http://localhost:8000/',
  // accessKeyId default can be used while using the downloadable version of DynamoDB.
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  accessKeyId: "fakeMyKeyId",
  // secretAccessKey default can be used while using the downloadable version of DynamoDB.
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  secretAccessKey: "fakeSecretAccessKey"
});

var dynamodb = new AWS.DynamoDB();

function createUsers() {
    var params = {
        TableName : "Users",
        KeySchema: [
            { AttributeName: "id", KeyType: "HASH"}
        ],
        AttributeDefinitions: [
            { AttributeName: "id", AttributeType: "N" }
        ],
        ProvisionedThroughput: {
            ReadCapacityUnits: 5,
            WriteCapacityUnits: 5
        }
    };

    dynamodb.createTable(params, function(err, data) {
        if (err) {
            document.getElementById('textarea').innerHTML = "Unable to create users table: " + "\n" + JSON.stringify(err, undefined, 2);
        } else {
            document.getElementById('textarea').innerHTML = "Created users table: " + "\n" + JSON.stringify(data, undefined, 2);
        }
    });
}

    </script>

    <title></title>
</head>
<body>

    <input id="createTableButton" type="button" value="Create Table" onclick="createUsers();" />
    <br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>


</body>
</html>

我已经为我们的用户表准备了样本JSON数据。我修改了他们的MoviesLoadTable.html文件,并将其转换为UsersLoadTable.html,它将JSON数据上传到DynamoDB本地,以便加载我的JSON用户的数据。

当我试图加载我的JSON数据时,我在控制台上得到以下错误:

代码语言:javascript
复制
Uncaught SyntaxError: Unexpected token : in JSON at position 497
    at JSON.parse (<anonymous>)
    at FileReader.r.onload (UsersLoadData.html:31)
r.onload @ UsersLoadData.html:31
FileReader (async)
processFile @ UsersLoadData.html:53

这是UsersLoadData.html

代码语言:javascript
复制
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />

    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>

    <script type="text/javascript">
AWS.config.update({
  region: "us-west-2",
  endpoint: 'http://localhost:8000/',
  // accessKeyId default can be used while using the downloadable version of DynamoDB.
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  accessKeyId: "fakeMyKeyId",
  // secretAccessKey default can be used while using the downloadable version of DynamoDB.
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  secretAccessKey: "fakeSecretAccessKey"
});

var docClient = new AWS.DynamoDB.DocumentClient();

function processFile(evt) {
    document.getElementById('textarea').innerHTML = "";
    document.getElementById('textarea').innerHTML += "Importing users into DynamoDB. Please wait..." + "\n";
    var file = evt.target.files[0];
    if (file) {
        var r = new FileReader();
        r.onload = function(e) {
            var contents = e.target.result;
            var allUsers = JSON.parse(contents);

            allUsers.forEach(function (user) {
                document.getElementById('textarea').innerHTML += "Processing user id: " + user.id + "\n";
                var params = {
                    TableName: "Users",
                    Item: {
                        "id": user.id,
                        "info": user.info
                    }
                };
                docClient.put(params, function (err, data) {
                    if (err) {
                        document.getElementById('textarea').innerHTML += "Unable to add user: " + count + user.id + "\n";
                        document.getElementById('textarea').innerHTML += "Error JSON: " + JSON.stringify(err) + "\n";
                    } else {
                        document.getElementById('textarea').innerHTML += "Loading succeeded(id): " + user.id + "\n";
                        textarea.scrollTop = textarea.scrollHeight;
                    }
                });
            });
    };
        r.readAsText(file);
    } else {
        alert("Could not read users data file");
    }
}

    </script>


    <title></title>
</head>
<body>

    <input type="file" id="fileinput" accept='application/json' />
    <br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>

    <script>
        document.getElementById('fileinput').addEventListener('change', processFile, false);
    </script>

</body>
</html>

我搜索了错误,但找不到令人满意的解决方案。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-09 16:17:30

您的JSON文件的格式无效:

这里你已经修复了JSON:

代码语言:javascript
复制
    [{
        "id": 1,
        "info": {
            "name": "John",
            "surname": "Smith",
            "city": "NY",
            "birthday": "26/07/1996",
            "activities": [
                "Basketball",
                "Cinema",
                "NightOut"
            ],
            "badges": [
                "Friendly Player",
                "Basketball Pro"
            ],
            "reviews": [
                "Came to event on time",
                "Good basketball player",
                "I didn' like him",
                "Didn't show up on time"
            ],
            "connections(id)": [
                2,
                3,
                4
            ],
            "events": [{
                    "place": "Some Place",
                    "date": "10/10/2017",
                    "time": "18:00",
                    "activity": "Basketball"
                },
                {
                    "place": "Another Place",
                    "date": "13/10/2017",
                    "time": "21:00",
                    "activity": "Cinema"
                },
                {
                    "place": "Third Place",
                    "date": "19/10/2017",
                    "time": "22:00",
                    "activity": "NightOut"
                }
            ]
        }
    },

    {
        "id": 2,
        "info": {
            "name": "Adam",
            "surname": "Williams",
            "city": "San Francisco",
            "birthday": "Unknown",
            "activities": [
                "Tennis",
                "NightOut"
            ],
            "badges": [
                "Friendly Player",
                "Tennis Pro"
            ],
            "reviews": [
                "Adam is the best",
                "Best tennis player ever",
                "Don't play tennis with this guy"
            ],
            "connections(id)": [
                1,
                3,
                4
            ],
            "events": [{
                    "place": "Tennis Place",
                    "date": "01/03/2018",
                    "time": "20:00",
                    "activity": "Tennis"
                },
                {
                    "place": "Nightout Place",
                    "date": "03/03/2018",
                    "time": "20:00",
                    "activity": "NightOut"
                }
            ]
        }
    },

    {
        "id": 3,
        "info": {
            "name": "Juan",
            "surname": "Martinez",
            "city": "New Mexico",
            "birthday": "Unknown",
            "activities": [
                "Basketball",
                "NightOut"
            ],
            "badges": [
                "Pro Basketballer",
                "Night Owl"
            ],
            "reviews": [
                "Juan is crazy",
                "This guy can drink more than an elephant"
            ],
            "connections(id)": [
                1,
                2,
                4
            ],
            "events": [{
                    "place": "Basketball Court",
                    "date": "25/02/2018",
                    "time": "16:00",
                    "activity": "Basketball"
                },
                {
                    "place": "Nighclub",
                    "date": "03/03/2018",
                    "time": "19:00",
                    "activity": "NightOut"
                }
            ]
        }
    },

    {
        "id": 4,
        "info": {
            "name": "Charles",
            "surname": "Jackson",
            "city": "Pennsylvania",
            "birthday": "Unknown",
            "activities": [
                "Coding",
                "Lecture"
            ],
            "badges": [
                "Lecturer of Lecturers",
                "Code Master"
            ],
            "reviews": [
                "Best lecturer in the world",
                "Codes are amazing"
            ],
            "connections(id)": [
                1,
                2,
                3
            ],
            "events": [{
                    "place": "Pennsylvania University",
                    "date": "05/03/2018",
                    "time": "13:00",
                    "activity": "Lecture"
                },
                {
                    "place": "Pennsylvania University",
                    "date": "05/03/2018",
                    "time": "16:00",
                    "activity": "Coding"
                }
            ]
        }
    }
]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49189207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档