JSON.parse()期间JSON中出现意外的令牌

时间:2016-07-24 06:33:44

标签: jquery json parsing object webrtc

对于webRTC优惠,我收到错误“位置26的JSON中的意外令牌”。我正在使用ajax调用从数据库获取提供并将其作为纯文本返回以解析为JSON对象。当我尝试解析字符串时,我收到一个“JSON中的意外令牌”错误。

这是JSON作为普通字符串

 {"type":"offer","sdp":"v=0
 o=- 552724588234335198 2 IN IP4 127.0.0.1
 s=-
 t=0 0
 a=msid-semantic: WMS
 m=application 52731 DTLS/SCTP 5000
 c=IN IP4 192.169.2.55
 a=candidate:2144433521 1 udp 2122257663 2002:c0a9:237::c0a9:237 52729 typ host generation 0 network-id 4
 a=candidate:1068658286 1 udp 2122189567 2001::9d38:90d7:88a:6fc:52ce:a035 52730 typ host generation 0 network-id 7
 a=candidate:398976708 1 udp 2122129151 192.169.2.55 52731 typ host generation 0 network-id 3
 a=candidate:827492737 1 tcp 1518277887 2002:c0a9:237::c0a9:237 9 typ host tcptype active generation 0 network-id 4
 a=candidate:1899074206 1 tcp 1518209791 2001::9d38:90d7:88a:6fc:52ce:a035 9 typ host tcptype active generation 0 network-id 7
 a=candidate:1497635380 1 tcp 1518149375 192.169.2.55 9 typ host tcptype active generation 0 network-id 3
 a=ice-ufrag:EHtolesxvVPp2FqI
 a=ice-pwd:CrcvrgKTp6lUbUA81nlGeTFr
 a=fingerprint:sha-256 01:39:AF:9A:67:87:F9:52:E9:20:3C:0D:4A:8B:A0:22:E2:D1:01:65:51:32:E1:5B:8B:9E:BC:CA:6E:DF:E2:46
 a=setup:actpass
 a=mid:data
 a=sctpmap:5000 webrtc-datachannel 1024
 "}

和代码

$.ajax({
    url: "./php/actions.php",
    type: "post",
    dataType: "text",
    data: {type:'retrieve', roomid:roomid},
    success: function(data){
        //data is plain text from string of offer retrieved from db
        $('.termp').text(data); //used to check raw text data for now
        var offerSDP = JSON.parse(data); //returning error when trying to convert to json
        remoteConn.setRemoteDescription(new RTCSessionDescription(offerSDP)); //needs an object
    }
});

我之前在另一个应用程序中使用复制和粘贴方法取得了成功,我在其中获取原始文本数据并将其粘贴到文本区域,然后将JSON.parse(text-area.val())成功设置为sessionDescription,但这些方法看似相同但产生了不同的结果。

1 个答案:

答案 0 :(得分:2)

在JSON中,禁止使用普通的新行字符。您需要将其替换为\n

请查看此主题:Multiline strings in JSON