简体   繁体   English

Append 到 JSONObject,使用 org.json 将 object 写入文件 ZD52387880E1EA22817A72D3752?

[英]Append to JSONObject, write object to file using org.json for Java?

To start, I have researched this extensively, but because org.json and json.simple use the same names for datatypes it is very confusing and does not apply to my specific instance.首先,我对此进行了广泛的研究,但由于 org.json 和 json.simple 对数据类型使用相同的名称,因此非常令人困惑,不适用于我的具体实例。 I need to take an existing JSONObject, append that object with another JSONObject, and write that new object to a file.我需要将现有的 JSONObject、append 和 object 与另一个 JSONObject 一起使用,并将新的 object 写入文件。 I can cast the other JSONObject to a String if that makes it easier to process.如果这样更容易处理,我可以将另一个 JSONObject 转换为字符串。

Example:例子:

JSONObject jObj = new JSONObject();
... make new JSONObject jo ...
jObj.put(jo);
... then somehow save the appended jObj to a file ...

If I understand correctly you're using org.json 如果我正确理解您使用的是org.json

In that case just use: 在这种情况下,只需使用:

PrintWriter myFile = new PrintWriter(myFilePath, "UTF-8");
String source = "{\"test\": \"my json object\"}";
JSONObject jObj = new JSONObject();
JSONObject jObjNew = new JSONObject(source);
jObj.put("jObjNew",jObjNew);
myFile.println(jObj.toString(4));

This is the output in the File at "myFilePath": 这是“ myFilePath”文件中的输出:

{
    "jObjNew": {
        "test": "my json object"
    }
}

The result from literally appending one JSONObject to another JSONObject would not be valid JSON.从字面上将一个 JSONObject 附加到另一个 JSONObject 的结果将不是有效的 JSON。 This is why the org.json library prevents you from doing precisely that.这就是为什么 org.json 库会阻止您这样做。 For example, take this valid JSON String:例如,使用这个有效的 JSON 字符串:

{ "Foo": "Bar" }

And append that to another valid another valid JSON String:和 append 到另一个有效的另一个有效的 JSON 字符串:

{ "Stack": "Overflow" }

Would result in invalid JSON:将导致无效的 JSON:

{ "Stack": "Overflow" }{ "Foo": "Bar" }

You can check JSON text for validity at https://jsonlint.com/您可以在https://jsonlint.com/上检查 JSON 文本的有效性

To combine those, you have at least two options:要结合这些,您至少有两个选择:

  1. Put both into one JSONObject, each with a uniquely named String key.将两者放入一个 JSONObject 中,每个都有一个唯一命名的 String 键。
  2. Put both into one JSONArray.将两者放入一个 JSONArray。

Here is the code for both of those solutions:以下是这两种解决方案的代码:

    JSONObject existingJSON = new JSONObject( "{ \"Foo\": \"Bar\" }" );
    JSONObject newJSON = new JSONObject( "{ \"Stack\": \"Overflow\" }" );
    
    // JSONObject solution:
    JSONObject combinedObject = new JSONObject();
    combinedObject.put( "newJSON", newJSON );
    combinedObject.put( "existingJSON", existingJSON );
    System.out.println( combinedObject );
    System.out.println( combinedObject.get( "newJSON" ) );
    System.out.println( combinedObject.get( "existingJSON" ) );

    // JSONArray solution:
    JSONArray combinedArray = new JSONArray();
    combinedArray.put( newJSON );
    combinedArray.put( existingJSON );
    System.out.println( combinedArray );
    System.out.println( combinedArray.get( 0 ) );
    System.out.println( combinedArray.get( 1 ) );

The output should look like this: output 应如下所示:

{"existingJSON":{"Foo":"Bar"},"newJSON":{"Stack":"Overflow"}}
{"Stack":"Overflow"}
{"Foo":"Bar"}
[{"Stack":"Overflow"},{"Foo":"Bar"}]
{"Stack":"Overflow"}
{"Foo":"Bar"}

As shown in the code above, you can access your two original JSONObjects using combinedObject.get( "keyname" ) or combinedArray.get( <array index #> ) .如上面的代码所示,您可以使用 combineObject.get combinedObject.get( "keyname" )combinedArray.get( <array index #> )访问您的两个原始 JSONObject。

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

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