如何使用 node.js restful API 查看 json 中的西里尔数据 [英] How to see cyrillic data in json use node.js restful API

查看:15
本文介绍了如何使用 node.js restful API 查看 json 中的西里尔数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 json 格式的西里尔文数据,在 API 中我看到这样的数据:

站0地图代码 0NAME_IMEBjala 河 - Smoljan"NAME_CYR "          -   ."警报级别网站 1代码 1

我想显示 NAME_CYR " - ." 使用 UTF-8 或 CP_1251 编码,但我不知道如何..?

代码:

 app.route('/stations').get(function (req, res) {//省略res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');pool.query(`SELECT * FROM sel_alert_level s;`,函数(错误,结果){如果(错误)return res.status(500).json({ error: "Грешна заявка.Опитай отново !"})站 = 结果;res.json({站})});});

我试着把 res.set({ 'content-type': 'application/json; charset=utf-8' }); before res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');像这样:

 app.route('/stations').get(function (req, res) {//省略res.set({ 'content-type': 'application/json; charset=utf-8' });res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');pool.query(`SELECT * FROM sel_alert_level s;`,函数(错误,结果){如果(错误)return res.status(500).json({ error: "Грешна заявка.Опитай отново !"})站 = 结果;res.json({站})});});

但这对我不起作用..

解决方案

您需要检查响应中的标头(如果您使用的是浏览器,请使用开发工具).JSON 默认为 UTF-8,如果您使用 express 应该没问题,但请仔细检查.

如果这不起作用 - 使用 res.set({ 'content-type': 'application/json; charset=utf-8' });(再次表达).

I have a cyrillic data in json format and in the API I see the data like this:

stations    
0   
Map_code    0
NAME_IME    "Bjala river - Smoljan"
NAME_CYR    "���� ���� - ��. ������"
Alertlevelwebsite   1
CODENUM 1

I want to display NAME_CYR "���� ���� - ��. ������" with UTF-8 or CP_1251 encode but I don't know how..?

The code:

    app.route('/stations')
  .get(function (req, res) {
    // omitted
    res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');

    pool.query(`SELECT * FROM sel_alert_level s;`, function (error, result) {
      if (error)
      return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
      stations = result;
      res.json({ stations })
    });
  });

I try to put res.set({ 'content-type': 'application/json; charset=utf-8' }); before res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate'); like this:

      app.route('/stations')
  .get(function (req, res) {
    // omitted
    res.set({ 'content-type': 'application/json; charset=utf-8' });
    res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');

    pool.query(`SELECT * FROM sel_alert_level s;`, function (error, result) {
      if (error)
      return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
      stations = result;
      res.json({ stations })
    });
  });

But this not work for me..

解决方案

You need to check the headers from the response (use dev tools if you're using the browser). JSON is UTF-8 by default and if you use express it should be fine, but just double check.

If that doesn't work - use res.set({ 'content-type': 'application/json; charset=utf-8' }); (Express again).

这篇关于如何使用 node.js restful API 查看 json 中的西里尔数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆