SyntaxError:JSON.parse:JavaScript中JSON数据的第1行第92列的JSON数据后出现意外的非空白字符 [英] SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 92 of the JSON data in javascript

查看:72
本文介绍了SyntaxError:JSON.parse:JavaScript中JSON数据的第1行第92列的JSON数据后出现意外的非空白字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaScript和AJAX的新手.我收到以下错误:

I am new to JavaScript and AJAX. I am getting the below error:

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 92 of the JSON data in javascript

我在Google中搜索了很多内容,但找不到解决方案.还有一件事,如果我在数据库中只有一个记录,那么我的代码可以正常工作;但是,如果我在数据库中插入另一行,则会收到错误消息.请帮助我解决此问题,请在下面找到我的代码:

I searched a lot in Google, but I can't find a solution. One more thing, if I have one only record in the database, my code works fine; but if I insert another row in the database, I get the error. Please help me to resolve this, find my code below:

 $.ajax({
   type: "POST",
   //dataType: "json",
   url: "./QuizServlet",

   success: function (responseText) {
     alert("alert" + responseText);

     var jsonData = JSON.parse(responseText);
     for (var i = 0; i < jsonData.length; i++) {
       var questions;     
       choice = jsonData[i].options;    
       ch = choice.split(","); 
       console.log(ch);
      questions = [{ question: questionnn, choices: ch, correctAnswer: answer }];
     }
   }
 });

同样,从数据库中获取多个行时,问题就来了.

Again, the issue is coming while fetching more than one row from database.

我已经在从JSON输出中重新添加了servlet代码.

i have added the servlet code from where is returing json output.

List<QuizDto> quiz=new ArrayList<QuizDto>();
    quiz=QuizDao.quizdetails();
    JSONArray jsonarray= new JSONArray();
    for(int i=0;i<quiz.size();i++)
    {
        JSONObject obj = new JSONObject();
        //obj.put("issueno", quiz.get(i).getIssueno());
        obj.put("questions",quiz.get(i).getQuestions());
        obj.put("answers",quiz.get(i).getAnswers());
        obj.put("options", quiz.get(i).getOptions());
        jsonarray.put(obj);
        System.out.println("Json in Servelt"+ jsonarray.toString());
        response.getWriter().print(jsonarray);
    }

我正在sysout中打印输出,它仅正确返回2个唯一记录.

i am prininting output in sysout, it is returning only 2 uniques records correctly.

Json in Servelt[{"answers":1,"questions":"what is correct answer","options":"first,second,third,fourth,"},{"answers":2,"questions":"what is quiz","options":"quiz,output,answer,question"}]

但是,在使用javascript时,ResponseText显示的1行重复了两次,如下所示:

but, while in javascript, ResponseText is displaying the 1 row repeated two times as shown below:

alert[{"answers":1,"questions":"what is correct answer","options":"first,second,third,fourth,"}][{"answers":1,"questions":"what is correct answer","options":"first,second,third,fourth,"},{"answers":2,"questions":"what is quiz","options":"quiz,output,answer,question"}]

不知道为什么会发生

推荐答案

您返回的响应可能不是JSON格式.最常见的问题是没有在对象键中添加引号.

The response you're returning is probably not in JSON format. The most common issue is not adding quotes in object keys.

根据您要发送的数据,您实际上是在发送几个串联的JSON(几个未封装且未用逗号分隔的数组).

According to the data you're sending, you're actually sending several concatenated JSONs (several arrays not encapsulated and not separated with commas).

这应该是 [[...],[...],[...]] ,而不是 [...] [...] [...] ,就像现在一样.

this should be [[...],[...],[...]] instead of [...][...][...] as you have now.

这篇关于SyntaxError:JSON.parse:JavaScript中JSON数据的第1行第92列的JSON数据后出现意外的非空白字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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