简体   繁体   English

java字符串到json元素的转换

[英]java string to json element conversion

I am trying to convert java string object into Jsonelement by given code , but getting error for line -- JsonElement jelement = ((Object) new JsonParser()).parser(result); 我试图通过给定的代码将java字符串对象转换为Jsonelement,但是获取行的错误 - JsonElement jelement =((Object)new JsonParser())。parser(result);

BufferedReader reader=new BufferedReader(new FileReader("/home/Priyanka/Documents/json/temp.json"));

    StringBuilder content=new StringBuilder();
    String result=null;
    String line = null;

        while ((line = reader.readLine()) != null) {
          content.append(line);
        }

     reader.close();
     result= content.toString();
     JsonElement jelement = ((Object) new JsonParser()).parser(result); // Error line

     printJsonRecursive(result);

As Fedy2 stated on comments you are casting JsonParser to Object. 正如Fedy2在评论中所述,您正在将JsonParser转换为Object。 Object doesn't have method parser() so it gives compilation error. 对象没有方法parser(),因此它给出了编译错误。 Just remove that cast and compilation error should be gone. 只需删除该转换,编译错误应该消失。

Your JSON is missing one brace } at the end. 你的JSON最后缺少一个大括号}。

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

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