解析包含unicode字符的JSON数据

时间:2018-01-09 21:54:41

标签: json utf-8 axios

我有 UTF8编码一个包含存储在AWS S3存储中的unicode字符的json文件。为了能够从我的React项目下载该文件,我创建了AWS Gateway API和Lambda函数。在项目中,我使用Axios库下载该文件,它似乎正确下载(我使用console.log检查内容)。但是,当我使用JSON.parse解析文件时,我得到了字符串文字中的错误控制字符"错误。使用JSON验证器检查内容返回肯定,并且API网关的http响应头被正确设置为" content-type:application / json"和" charset:utf-8"。 JSON.parse仅在我从文件中删除所有UTF8 unicode字符时才有效。当我删除包含unicode字符的部分时,我甚至不需要使用JSON解析。我可以从我的脚本中将其作为对象访问。这是我的json文件的内容

{
  "en": [
    {
      "Question": "Question 1",
      "Choice": [ "Strongly  Agree", "Agree", "Neither Agree Nor Disagree", "Disagree", "Strongly Disagree" ]
    },
    {
      "Question": "Question 2",
      "Choice": [ "Strongly  Agree", "Agree", "Neither Agree Nor Disagree", "Disagree", "Strongly Disagree" ]
    },
    {
      "Question": "Question 3",
      "Choice": [ "Strongly  Agree", "Agree", "Neither Agree Nor Disagree", "Disagree", "Strongly Disagree" ]
    },
    {
      "Question": "Question 4",
      "Choice": [ "Strongly  Agree", "Agree", "Neither Agree Nor Disagree", "Disagree", "Strongly Disagree" ]
    }
  ],
  "fr": [
    {
      "Question": "Question 1",
      "Choice": [ "Tout À Fait d'Accord", "d'Accord", "Ni En Désaccord Ni d'Accord", "Pas d'Accord", "Pas Du Tout d'Accord"]
    },
    {
      "Question": "Question 2",
      "Choice": [ "Tout À Fait d'Accord", "d'Accord", "Ni En Désaccord Ni d'Accord", "Pas d'Accord", "Pas Du Tout d'Accord"]
    },
    {
      "Question": "Question 3",
      "Choice": [ "Tout À Fait d'Accord", "d'Accord", "Ni En Désaccord Ni d'Accord", "Pas d'Accord", "Pas Du Tout d'Accord"]
    },
    {
      "Question": "Question 4",
      "Choice": [ "Tout À Fait d'Accord", "d'Accord", "Ni En Désaccord Ni d'Accord", "Pas d'Accord", "Pas Du Tout d'Accord"]
    }
  ],
  "pt": [
    {
      "Question": "Questão 1",
      "Choice": [ "Concordo Plenamente", "Aceita", "Não Concordo Nem Discordo", "Discordar", "Discordo Fortemente" ]
    },
    {
      "Question": "Questão 2",
      "Choice": [ "Concordo Plenamente", "Aceita", "Não Concordo Nem Discordo", "Discordar", "Discordo Fortemente" ]
    },
    {
      "Question": "Questão 3",
      "Choice": [ "Concordo Plenamente", "Aceita", "Não Concordo Nem Discordo", "Discordar", "Discordo Fortemente" ]
    },
    {
      "Question": "Questão 4",
      "Choice": [ "Concordo Plenamente", "Aceita", "Não Concordo Nem Discordo", "Discordar", "Discordo Fortemente" ]
    }
  ],
  "my": [
    {
      "Question": "မေးခွန်း ၁",
      "Choice": [ "အပြည့်အ၀ထောက်ခံတယ်", "ထောက်ခံတယ်", "ထောက်ခံတယ်လည်းမဟုတ်ဘူး မထောက်ခံတယ်လည်းမဟုတ်ဘူး", "မထောက်ခံဘူး", "အပြည့်အ၀မထောက်ခံဘူး" ]
    },
    {
      "Question": "မေးခွန်း ၂",
      "Choice": [ "Strongly  Disagree", "Somewhat Disagree", "Agree", "Somewhat Agree", "Strongly Agree" ]
    },
    {
      "Question": "မေးခွန်း ၃",
      "Choice": [ "Strongly  Disagree", "Somewhat Disagree", "Agree", "Somewhat Agree", "Strongly Agree" ]
    },
    {
      "Question": "မေးခွန်း ၄",
      "Choice": [ "Strongly  Disagree", "Somewhat Disagree", "Agree", "Somewhat Agree", "Strongly Agree" ]
    }
  ]
}

修改 这是负责下载和解析该文件的代码:

    let request = {
        host: process.env.AWS_HOST,
        method: 'GET',
        url: process.env.AWS_URL,
        path: process.env.AWS_PATH
    }

    let signedRequest = aws4.sign(request, {
        secretAccessKey: process.env.AWS_SECRET_KEY,
        accessKeyId: process.env.AWS_ACCESS_KEY
     });


    axios(signedRequest)
        .then(response => {

            console.log(response.data); 

            JSON.parse(response.data); // Error!

        })
        .catch((error) => {
            console.log("error",error);
        });

修改 我更正了标题,以反映我提出的问题。

1 个答案:

答案 0 :(得分:1)

我只是发现这是我的lambda函数导致了这个问题。该函数从s3存储桶读取文件,并在返回为响应之前将数据编码为ascii。修复它到utf-8解决问题。感谢@Tomalak您的时间。

select
  Q.Quiz_Name Quiz
 ,S.LastName Last
 ,S.FirstName First
 ,QD.Quiz_ID
 ,QD.Student_ID
from 
/* Get a full list of ALL Test/Student combinations */
           quiz Q 
CROSS JOIN student S 
/* Join the taken tests to the combinations */
 LEFT JOIN quiz_details QD on Q.id = QD.quiz_id
                          and S.id = QD.student_id
/* Only select where no Quiz_ID exists */
WHERE QD.Quiz_ID IS NULL
ORDER BY Q.Quiz_Name, S.Lastname, S.FirstName;