简体   繁体   English

使用JSON.parse错误服务器=>客户端node.js进行解析

[英]Parse with JSON.parse error server=>client node.js

I send a request from the client to the server, and I got this response: 我从客户端向服务器发送了一个请求,并且得到了以下响应:

{
    "m": "",
    "d": {
        "e": [
            {
                "k": "login",
                "m": "__10_10"
            }
        ]
    },
    "s": false,
    "t": "v"
}

But when I try to do JSON.parse on this response from client side I got: 但是当我尝试从客户端的响应中执行JSON.parse时,我得到了:

Uncaught Message._fromJSONString: JSON.parse error:undefined 

Code: 码:

    try{
        console.log(json);
        return Message._fromJSONObject(JSON.parse(json));
    }catch(e){
        throw "Message._fromJSONString: JSON.parse error:" + e.message;
    }

I check my JSON from http://jsonlint.com/ and it's ok... I really don't understand this issue, it's a special class who send this response, a Validator. 我从http://jsonlint.com/检查我的JSON,没关系...我真的不明白这个问题,这是发送此响应的一个特殊类,即Validator。 From other requests I don't have this problem... 根据其他要求,我没有这个问题...

Do you have any idea about what's goig on? 您有什么想法吗? Thanks. 谢谢。

I have found a solution to your problem. 我找到了解决您问题的方法。 It seems that for some reason it was taking it as normal JavaScript Object instead of JSON. 似乎出于某种原因,它把它当作普通的JavaScript对象而不是JSON。 This code works fine now. 该代码现在可以正常工作。

var json ={
    "m": "",
    "d": {
        "e": [
            {
                "k": "login",
                "m": "__10_10"
            }
        ]
    },
    "s": "false",
    "t": "v"
};

 json =JSON.stringify(json); // this converts it into JSON parsable
console.log(JSON.parse(json));

JSfiddle => http://jsfiddle.net/j39tP/ JSfiddle => http://jsfiddle.net/j39tP/

I experienced an error similar to this this morning. 我今天早上遇到了类似的错误。 I had to do two things to fix it: 我必须做两件事来修复它:

  1. For whatever reason, the error log was saying that I needed to put the json data in an array format, like this JSON.parse([jsondata]); 无论出于什么原因,错误日志都说我需要将json数据放入数组格式,例如JSON.parse([jsondata]);。

  2. On top of my, my server side output had an error as well - so I guess double check that to make sure it's returning the correct result? 最重要的是,我的服务器端输出也有错误-所以我想仔细检查一下以确保它返回正确的结果?

Not sure if this applies to your situation. 不知道这是否适用于您的情况。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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