看起来好像是在尝试使用Java解析JSON.为什么不使用特定的库?
像基本的
http://www.json.org/java/或谷歌的
https://github.com/google/gson
否则,我不认为你提出的复杂结构特别慢.如果您认为更有效或更容易获取数据,您可能希望设计自己的对象类来保存数据.
编辑
基于你的问题,我认为JSON是你收到它的格式,抱歉.
我只想为GenericRecord创建一个包装器,或者将它子类化.然后添加提取数据所需的方法,或使其成为可比较的排序方法.
有点像
public class MyRecord extends GenericRecord implements Comparable
{
// Determine the type
public int getType()
{
if ( this.get( "id") != null )
return 2;
return 1;
}
// Add methods that allow you to retrieve the serial field from any of the two record types
public String getId()
{
if ( this.get( "id") != null )
return (String)this.get("id");
return (String)this.get("serial_no");
}
// add comparator methods that will allow you to sort the list, compare based on Id, etc
@Override
public int compareTo(MyRecord another)
{
// Just a simple example
return this.getId().compareTo( another.getId() );
}
}