Java:生成JSON:如何摆脱 - 生成不完整的JSON

时间:2015-05-18 18:55:57

标签: java json

在下面的Java代码中,我试图生成JSON,它应该如下所示: 在运行程序时,我得到以下异常:
更新:我刚刚解决了我的问题,以下异常消失了:更新的代码在下面
第50行:对应于下面列出的Java代码中的 generator.close()

Exception in thread "main" javax.json.stream.JsonGenerationException: Generating incomplete JSON
    at org.glassfish.json.JsonGeneratorImpl.close(JsonGeneratorImpl.java:509)
    at com.myhome.myapp.GenerateJson.main(GenerateJson.java:50)


有人能指出我在弄乱代码的地方以及如何纠正它吗?非常感谢。

{
        "X-Awesome-Results": [
            {
                "results": {
                    "subjectLine": "Hello, Hello",
                    "Rule": "Block Sadness",
                    "match": "true",
                    "content": {
                        "scanResult": "Detected unhappiness that needs to be cleaned out "
                    }
                }
            }
        ]
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
 import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
 Jars used are:
    1) gson-2.3.1.jar
     2) jackson-annotations-2.5.1.jar
      3) jackson-core-2.5.1.jar
      4)jackson-databind-2.5.1.jar
      5)javax.json-1.0.4.jar
      6)javax.json-api-1.0.jar
      7)json-simple-1.1.1.jar
*/


public class GenerateJson {

    public static void main(String[] args) {

        FileWriter writer = null;
        JSONParser parser = new JSONParser();
        Object simpleObj = null;

        try {
                //File to write the generated json into
            writer = new FileWriter("C:/path/to/storing/generated/json/jsonFormatted.json");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // JsonGenerator to create JSONObject and store it to file location mentioned above
        JsonGenerator generator = Json.createGenerator(writer);

        generator.writeStartObject().writeStartArray("X-Awesome-Results")
    .writeStartObject()
         .writeStartObject("results")
            //.writeStartObject()
                .write("subjectLine", "Hello, Hello")
                .write("policyEntryOrRule", "Block Sadness")
                .write("match", "true")
        .writeStartObject("content")
            .write("scanResult", "Detected sadness. Need more optimism")
            .writeEnd()
        .writeEnd() 
    .writeEnd()
    .writeEnd()
    .writeEnd();

        generator.close();

try {
            simpleObj = parser.parse(new FileReader("C:/path/to/storing/generated/json/jsonFormatted.json"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("Simple JSON Result:\n" + simpleObj.toString());

            String prettyFinalJson = myPrettyJSONUtility(finalJson);
            System.out.println("\nPretty JSON Result:\n" + prettyFinalJson);

        }

        public static String myPrettyJSONUtility(String simpleJSON) {
            JsonParser avidParser = new JsonParser();
            JsonObject json = avidParser.parse(simpleJSON).getAsJsonObject();

            Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
            String prettyJson = prettyGson.toJson(json);

            return prettyJson;
        }


    }

0 个答案:

没有答案