简体   繁体   English

生成JSONObject中的搜索值?

[英]Generifying search values in JSONObject?

I have a quite varying structure JSONObject. 我有一个相当不同的结构JSONObject。 I am using json.simple to get values from the object like this: 我正在使用json.simple从这样的对象获取值:

String val1 = ((LinkedHashMap<String, String>) response.get("key1")).get("inner_key1");
String val2 = ((LinkedHashMap<String, String>) response.get("key1")).get("inner_key2");
int index1 = (int) ((ArrayList) response.get("index_key1")).get(0);
int index2 = (int) ((ArrayList) response.get("index_key2")).get(0);
int index3 = (int) ((ArrayList) response.get("index_key3")).get(0);

I am wondering if I can use a utility method to get these values with a better, more generic way? 我想知道是否可以使用实用程序方法以更好,更通用的方式获取这些值? Also, I would like to avoid those casting too, is there any solution for that? 另外,我也想避免这些转换,是否有解决方案?

Since the keys in JSON are always the same you can create a class and specify the keys as instance varaibles. 由于JSON中的键始终相同,因此您可以创建一个类并将这些键指定为实例变量。 Now you can use Jackson library's Object Mapper to convert the JSON string to JSON Object as shown in below code. 现在,您可以使用Jackson library's Object MapperJSON string转换为JSON Object ,如下面的代码所示。

public class Vehicle{
private String brand = null;
private int doors = 0;

public String getBrand() { return this.brand; }
public void   setBrand(String brand){ this.brand = brand;}

public int  getDoors() { return this.doors; }
public void setDoors (int doors) { this.doors = doors; }
}

Insdie the main function you can write the below code: 插入主要功能可以编写以下代码:

ObjectMapper objectMapper = new ObjectMapper();

String carJson =
"{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";

try {
Vehicle vehicle= objectMapper.readValue(carJson, Vehicle.class);

System.out.println("car brand = " + vehicle.getBrand());
System.out.println("car doors = " + vehicle.getDoors());
} catch (IOException e) {
e.printStackTrace();
}

Also, using Object Mapper way you don't need to do the cast. 另外,使用Object Mapper方式,您无需执行转换。 You can read more about it here . 您可以在此处了解更多信息。

You could use GSON library provided by Google to map JSON to Java. 您可以使用Google提供的GSON库将JSON映射到Java。

An example, 一个例子,

public class Example {

  private String val1;
  private String val2;
  private ArrayList<int> index_key1;
  private ArrayList<int> index_key2;
  private ArrayList<int> index_key3;

  ///
  //  Getter Methods
  ///

  public String getVal1() { return val1; }
  public String getVal2() { return val2; }
  public int getIndex1() { return index_key1.get(0); }
  public int getIndex2() { return index_key2.get(0); }
  public int getIndex3() { return index_key3.get(0); }
}

Then 然后

  Example ex = new Gson().fromJson(response.toString(), Example .class);  

After that you should be good to go. 之后,您应该会很好。 There also is a library called Jackson which provides a similar solution to GSON. 还有一个名为Jackson的库,它提供了与GSON类似的解决方案。

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

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