0

次のエラーが表示されます。

キャッチされない例外: 無効な JSON: {"id":1,"channel":"V125954","text":"{"nick":"Du","visit":"1","text":"hello" ,"_ref":"デュ","_cur":"デュ","_ip":"デュ","_browser":"デュ","_os":"デュ","_td":"12:29" }"}

次の関数で解析しようとすると:

  var parseJSON = function(data) {
    if (!data || !isString(data)) {
      return null;
    }

    // Make sure leading/trailing whitespace is removed (IE can't handle it)
    data = trim(data);

    // Attempt to parse using the native JSON parser first
    if (window.JSON && window.JSON.parse) {
      try {
        return window.JSON.parse( data );
      } catch(e) {
        throw "Invalid JSON: " + data;
        console.log(e);
      }
    }

    // Make sure the incoming data is actual JSON
    // Logic borrowed from http://json.org/json2.js
    if (validChars.test(data.replace(validEscape, "@").replace( validTokens, "]").replace( validBraces, "")) ) {
      return (new Function("return " + data))();
    }

    throw "Invalid JSON: " + data;
  };

データは nodejs を介して次のように送信されます。

        var options = {
          uri: 'http://localhost/pub?id=' + req.params.channel,
          method: 'POST',
          json: {
            "nick": "Du",
            "visit": "1",
            "text": "hej",
            "_ref": "Du",
            "_cur": "Du",
            "_ip": "Du",
            "_browser": "Du",
            "_os": "Du",
            "_td": "12:29",                                                                                                                                                             
          }
        };

        request_helper(options, function (error, response, body) {
          if (!error && response.statusCode == 200) {
            console.log("ok") 
          }
        }); 

何が間違っている可能性がありますか?

4

3 に答える 3

0

データに問題がある

{"id":1,"channel":"V125954","text":"{"nick":"Du","visit":"1","text":"hello","_ref":"Du","_cur":"Du","_ip":"Du","_browser":"Du","_os":"Du","_td":"12:29"}"}

"text": "{"nick":"Du...この部分にはエラー\"nickがあります

{"id":1,"channel":"V125954","text":'{"nick":"Du","visit":"1","text":"hello","_ref":"Du","_cur":"Du","_ip":"Du","_browser":"Du","_os":"Du","_td":"12:29"}'};
于 2013-02-21T10:20:21.737 に答える
0

json が無効です。

http://jsonlint.com/を使用 して、json ファイルを検証します。

修正されたjson

{
    "id": 1,
    "channel": "V125954",
    "text": {
        "nick": "Du",
        "visit": "1",
        "text": "hello",
        "_ref": "Du",
        "_cur": "Du",
        "_ip": "Du",
        "_browser": "Du",
        "_os": "Du",
        "_td": "12: 29"
    }
}
于 2013-02-21T10:18:06.000 に答える