使用GSON解析动态&QUOT一个JSON;密钥QUOT;和"值QUOT;在机器人

问题描述:

我目前工作的一个Android项目,需要我调用Web服务将返回我一个JSON文件。我一直在使用GSON库来解析所有的JSON文件,并得到一个JSON对象。它一直很好,直到我碰到过这样的JSON数据,重点领域是动态的。这个文件的一个例子是如下:

I am currently working on an android project that requires me to invoke a web service that will return me a json file. I have been using GSON library to parse all the json file and get a JSON object. It has been working well until I come across this json data which the key fields are dynamic. An example of this file is as below:

{ "0": { "count":"5"},
  "1": { "title":"...", "desc":"" },
  "2": { "title":"...", "desc":"" },
  "3": { "title":"...", "desc":"" },
  "4": { "title":"...", "desc":"" },
  "5": { "title":"...", "desc":"" },
  "routes": { "route1":"...", "route3":"" },
}

我能够依据0键ID来获得计数,但我不知道我该如何利用这个值来获得其他主要对象(密钥ID 1-5),其中包括我所需要的数据。将真正AP preciate如果有人能帮助我在这件事情。谢谢你。

I am able to get the count based on the key id of "0", but I am not sure how do I make use of this value to get the other key objects (key id 1-5), which consist of the data that I needed. Will really appreciate if anyone is able to help me in this matter. Thanks.

最直接的方法我能想到的是,只是把结构为(的地图 地图)。

The most straightforward approach I can think of is to just treat the structure as a Map (of Map).

通过GSON,这是比较容易做到,只要地图结构是静态已知的,从根本上每个分支都有相同的深度,并且一切都是字符串

With Gson, this is relatively easy to do, as long as the Map structure is statically known, every branch from the root has the same depth, and everything is a String.

import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonFoo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Type mapType = new TypeToken<Map<String,Map<String, String>>>() {}.getType();
    Map<String,Map<String, String>> map = gson.fromJson(new FileReader("input.json"), mapType);
    System.out.println(map);

    // Get the count...
    int count = Integer.parseInt(map.get("0").get("count"));

    // Get each numbered entry...
    for (int i = 1; i <= count; i++)
    {
      System.out.println("Entry " + i + ":");
      Map<String, String> numberedEntry = map.get(String.valueOf(i));
      for (String key : numberedEntry.keySet())
        System.out.printf("key=%s, value=%s\n", key, numberedEntry.get(key));
    }

    // Get the routes...
    Map<String, String> routes = map.get("routes");

    // Get each route...
    System.out.println("Routes:");
    for (String key : routes.keySet())
      System.out.printf("key=%s, value=%s\n", key, routes.get(key));
  }
}

有关更多的动态地图结构处理,我强烈建议切换到使用杰克逊,因为杰克逊将反序列化的任意复杂的任何JSON对象转换为Java 地图,用code只是一个简单的线条,它会自动保留原始值的类型。

For more dynamic Map structure handling, I strongly suggest switching to use Jackson, instead of Gson, as Jackson will deserialize any JSON object of any arbitrary complexity into a Java Map, with just one simple line of code, and it will automatically retain the types of primitive values.

import java.io.File;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

public class JacksonFoo
{
  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    Map map = mapper.readValue(new File("input.json"), Map.class);
    System.out.println(map);
  }
}

同样可以GSON可以实现,但它需要几十code线。 (另外,GSON具有使切换到杰克逊非常值得等缺点。)

The same can be achieved with Gson, but it requires dozens of lines of code. (Plus, Gson has other shortcomings that make switching to Jackson well worth it.)