Pregunta

I am trying to parse a field of return JSON data, from an API which has a lot of strange characters in it (East Asian Symbols, curly quotes etc.). I am getting this error and do not know how to fix it. Is there a way to convert the request response to some format that "escapes" the bad text. Here is the error:

Here is my exact code, I am sorry it is so long.

var fs = require('fs'),
    http = require('http'),
    request = require('request');

var url = 'http://app.sportstream.com/api/tagstream/tagStreamForStageTwoModeration?q={%22customerId%22:%22ABHoko14%22,%22type%22:{%22$in%22:[%22image%22,%22video%22]}}&_=_5555'
var firstTime = false
var m = 1

    function get() {
        console.log('ghghg')
        http.get(url, function(res) {
            var body = '';
            res.on('data', function(chunk) {
                body += chunk;
            });
            res.on('end', function() {
                body = JSON.parse(body)
                jParse(body)
                setTimeout(get, 5000)
            });
        }).on('error', function(e) {
            console.log("Got error: ", e);
            setTimeout(get, 5000)
        });
    }

    function jParse(info) {
        //data = JSON.parse(info)
        data = info
        entries = data.entries
        if (firstTime) {
            numEntries = 800 //entries.length
            firstTime = false
        } else {
            numEntries = 2
            //numEntries = entries.length - numEntries
            if (numEntries) {
                for (i = 0; i < numEntries; i++) {
                    type = entries[i]['type']
                    title = entries[i]['author']
                    if (type == 'video') {
                        url = entries[i]['ssMetaData']['videos']['standard_resolution']['url']
                        download(url, 'images/aFile.mp4', function() {
                            console.log('hello')
                        })
                    } else if (type == 'image') {
                        url = entries[i]['ssMetaData']['images']['standard_resolution']['url']
                        download(url, 'images/' + m + 'File.jpg', function() {
                            console.log('hello')
                        })

                    } else {
                        console.log('no data')
                    }
                    m++
                }
            }
        }
    }

get()

And here is my error

undefined:1
����
^
SyntaxError: Unexpected token �
at Object.parse (native)
¿Fue útil?

Solución

By foreign characters; I assume you are referring to UTF-8 encoded string.

Why don't you try setting the Content-Type encoding on the response like this:

http.get(url, function(res) {
    res.header("Content-Type", "application/json; charset=utf-8");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top