English 中文(简体)
如何整理JSON档案?
原标题:How to parse JSON file?
  • 时间:2010-06-08 19:55:50
  •  标签:
  • java
  • json

简单情况:

  1. read a json file
  2. discover all key-value pairs
  3. compare key-value pairs

我尝试了格森,包裹从json.org开始,但似乎无法继续。

有些人请在 Java提供如何取用档案的清晰样本,读到,最后,我可以从中获取关键/价值。

考虑:

private void runThroughJson(JsonObject jsonObject) {
    for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {

        final String key = entry.getKey();
        final JsonElement value = entry.getValue();

        System.out.println(key + " - " + value);

        if (value.isJsonObject()) {
            runThroughJson(value.getAsJsonObject());
        } else {                
            int ix = value.getAsString().indexOf( [ );
            int ig = value.getAsString().lastIndexOf( ] );

            System.out.println(ix);
            System.out.println(ig);

            String a = value.getAsString().substring(ix, ig);
            JsonElement jsonElement = parser.parse(a);
            runThroughJson(jsonElement.getAsJsonObject());
        }
    }
}

但从逻辑上看,这似乎是一种例外:

Exception in thread "main" java.lang.IllegalStateException
    at com.google.gson.JsonArray.getAsString(JsonArray.java:133)
    at com.cme.esg.bk.TryGson.runThroughJson(TryGson.java:46)
    at com.cme.esg.bk.TryGson.runThroughJson(TryGson.java:44)
    at com.cme.esg.bk.TryGson.goForIt(TryGson.java:32)
    at com.cme.esg.bk.TryGson.main(TryGson.java:16)

您可以告知,是失踪的。

最佳回答

用Gson(假设你在座标上{......},在您的json档案顶层:

final JsonParser parser = new JsonParser();
final JsonElement jsonElement = parser.parse(new FileReader("/path/to/myfile"));
final JsonObject jsonObject = jsonElement.getAsJsonObject();

for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
   final String key = entry.getKey();
   final JsonElement value = entry.getValue();
   ....
}

In response to your comment:

你当然应该避免把 j子从扼杀中再par。 使用类似的东西:

... else if (value.isJsonArray()) {
   final JsonArray jsonArray = value.getAsJsonArray();
   if (jsonArray.size() == 1) {
      runThroughJson(jsonArray.get(0));
   } else {
        // perform some error handling, since
        // you expect it to have just one child!
   }

} 
问题回答

我们使用Jaskson parser,这里是样本法:

protected T getJsonObject(InputStream inputStream, Class<T> className) throws JsonParseException,
      JsonMappingException, IOException {
    // Deserialize input to Json object
    ObjectMapper mapper = new ObjectMapper();

    T jsonSource = mapper.readValue(inputStream, className);
    return jsonSource;
}

这里是法律如何援引:

JsonEmployee jsonEmployee = getJsonObject(inputStream, JsonEmployee.class);

JsonEmployee.java只是POJO

XStream is good for JSON: http://x-stream.github.io/json-tutorial.html

由于XStream具有灵活的结构,处理JSON地图像处理XML文件一样容易。 你们必须做的是,先把XStream物体与适当的驾驶员一起发射,你准备将物体与(和来自)JSON相序列。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签