【问题标题】:Unexpected token LEFT BRACE({) at position 4位置 4 处的意外令牌左括号({)
【发布时间】:2019-01-11 00:23:55
【问题描述】:

我正在研究 Spring Framework,我正在解析 JSON 数据,但它显示:

在位置 4 出现意外的标记 LEFT BRACE({)。

但是 eclipse 没有显示任何错误迹象。

这是我的控制器源代码:

@RequestMapping(value = "/listcall.do", method = RequestMethod.GET)
public void home( @RequestParam("val") String id, HttpServletRequest request , HttpServletResponse response) throws JsonIOException, IOException, ParseException {
    response.setCharacterEncoding("utf8");
    response.setContentType("application/json");
    String valuewhat = id ;
    JsonArray jarraySend = new JsonArray();
    //System.out.println(request.getParameter("val"));
    JSONParser parse = new JSONParser();
    FileReader fileReader = new FileReader("D:\\Spring\\sworkspace\\Calendar\\src\\main\\resources\\somelist.json");
    Scanner sc = new Scanner(fileReader);
    String inline = null;
    while(sc.hasNext())
    {
    inline+=sc.nextLine();
    }
    JSONObject json = (JSONObject) parse.parse(inline);
    JSONArray jarray_1 = (JSONArray) json.get("listing_property_type_information");

    for(int i=0;i<jarray_1.size();i++) {
        JSONObject jsonobj_1 = (JSONObject)jarray_1.get(i);

        //JsonArray jsonarr_2 = (JsonArray) jsonobj_1.get("property_type_groups");
        jarray_1 = (JSONArray) jsonobj_1.get("property_type_groups");
        for(int j=0;j<jarray_1.size();j++) {
            JSONObject jsonobj_2 = (JSONObject) jarray_1.get(j);

            //something do.....

        }
    }


    //jarray.add(json);
    new Gson().toJson(jarray_1, response.getWriter());
}

我一直在练习模仿 AirBNB 网站等功能。

JSON 文件位于:

https://www.airbnb.co.kr/become-a-host/room

【问题讨论】:

    标签: java json ajax spring spring-mvc


    【解决方案1】:

    你应该看看你的inline变量。你应该像thisString inline = "";这样声明这个变量,而不是String inline = null;,这将使你拼接的字符串以“null”开头

    【讨论】:

      【解决方案2】:

      这意味着你的 json 是坏的。 Eclipse 无法捕获它,因为它是运行时错误。

      【讨论】:

      • 哦 Eclipse 不是捕获 json ...但我知道有时会向 json 文件显示错误,这是 ecilpse 错误?
      【解决方案3】:
      public static  String fetchFromJSONNext() throws FileNotFoundException, IOException, org.json.simple.parser.ParseException  {
      
      
      
      //      String filename=System.getProperty("user.dir")+"\\src\\com\\test\\mtcn1.json";
      
          String filename=System.getProperty("user.dir")+"\\src\\com\\test\\mtcn1.json";
      
      
          //System.out.println(request.getParameter("val"));
          JSONParser parse = new JSONParser();
          FileReader fileReader = new FileReader(filename);
          Scanner sc = new Scanner(fileReader);
          String inline ="";
          String sj ="";
      
          while(sc.hasNext()){
              inline+=sc.nextLine();
      
              JSONObject jsonobj_2 = (JSONObject) parse.parse(inline);
      
      
                      //something do.....
      
                      Object g=jsonobj_2.get("result");
                      JSONObject h = (JSONObject) g;
                      sj=(String) h.get("wutaf_rpcmsg");
      
          }
          return sj;
      }
      

      【讨论】:

      • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
      猜你喜欢
      • 1970-01-01
      • 2021-07-01
      • 2016-08-17
      • 1970-01-01
      • 2018-06-10
      • 2016-12-13
      • 1970-01-01
      • 2018-11-08
      • 2021-07-11
      相关资源
      最近更新 更多