【问题标题】:Implementing JSON Library Java - ToString function实现 JSON 库 Java - ToString 函数
【发布时间】:2016-10-07 22:08:17
【问题描述】:

我正在处理一项实验室任务,在第一部分中,我需要实现一个简单的 JSON 库,以便可以在 Java 中导出 JSON 数据。

现在我被 JSONobject toString() 函数困住了;

该库应该可以将 JSON 模型导出为文本表示形式,以便将其存储在文件中,或使用 HTTP 传输,因此我需要使用 toString 方法

但是我已经遇到了错误,有人可以帮助我使用 toString 函数

这是我的 JsonObject 类:

import java.util.LinkedHashMap;

public class JsonObject extends LinkedHashMap<String, JsonValue> implements JsonValue {


   @Override
   public JsonObject asObject(){

       return this;
   }

   public void put(String key, int i){

       JsonPrimitive jsonprim = new JsonPrimitive(i);
       this.put(key, jsonprim);


   }

   public void put(String key, boolean b){

         JsonPrimitive jsonprim = new JsonPrimitive(b);
       this.put(key, jsonprim);

   }
   public void put(String key, double d){

         JsonPrimitive jsonprim = new JsonPrimitive(d);
       this.put(key, jsonprim);

   }

  public void put(String key, String string){

        JsonPrimitive jsonprim = new JsonPrimitive(string);
       this.put(key, jsonprim);

   }

  public JsonValue get(String key){
        throw new UnsupportedOperationException(
                "Cannot return JsonValue of type " + this.getClass().getSimpleName() + "as string");

  }

  public boolean has(String key) {
         throw new UnsupportedOperationException(
                "Cannot return JsonValue of type " + this.getClass().getSimpleName() + "as string");

  }

  public JsonValue get(int index) {
     return this.get(index);
  }

这是我卡住的 toString

public String toString() {

      return this.toString();
  }
}

&主要方法

public static void main(String[] args) {
       //Create a nested JSON object containing user records
        JsonArray userRecords = new JsonArray();
        userRecords.add(new JsonObjectBuilder()
              .add("id", 1)
               .add("name", "John Doe")
                .add("address", new JsonObjectBuilder()
                      .add("street", "Abbey Road")
                       .add("number", 7)
                       .add("city", "London"))
               .build());

    userRecords.add(new JsonObjectBuilder()
            .add("id", 2)
            .add("name", "Jane Doe")
            .add("address", new JsonObjectBuilder()
                    .add("street", "Abbey Road")
                    .add("number", 8)
                    .add("city", "London"))
            .build());

    //Print the records
    System.out.println(userRecords);

    //Retrieve the address of the first user
    System.out.println(userRecords.get(0).get("address"));

【问题讨论】:

  • 您遇到什么错误?实际结果是什么?您的预期是什么?
  • 我应该得到我在 main 中硬编码的记录的 json 代码,但我可以构建项目

标签: java json tostring


【解决方案1】:

您的错误是“java.lang.StackOverflowError”吗?

如果是这样,你的 toString() 方法就是原因。你在写

public String toString() {
      return this.toString();
  }
}

应该是这样的

public String toString() {
      return super.toString();
  }
}

或其他一些实现。 (至少,不要调用 this.toString())

因为这里的“this”指向你的 JsonObject 类,而 this.toString() 就是这个方法本身。

所以在 toString() 方法中调用 this.toString() 会导致 infinit 方法调用。

【讨论】:

  • 如果它只是return super.toString();,所以根本不要覆盖它。
  • 我只是想说“在 toString() 方法中调用 this.toString() 是错误的”。也许这里需要一些其他的实现。
  • 谢谢!但是错误是 Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project Labo1: Command execution failed。进程退出并出现错误:1(退出值:1)-> [Help 1]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多