2013-03-19 69 views
0

我无法获得JSON和Java对象一起工作的要点。基本上我需要做的是检查一个URL的JSON文件,看看是否有更新版本的软件可供使用。我打开了连接,并可以将JSON打印到控制台,但这就是我运气好的一切。我一直在尝试使用GSON。下面是我迄今为止的主要代码,其中清单是类的对象,显示在更下面。我最关心的是JSON中的“版本”字段,因为我需要将它与我当前的程序版本号进行比较。在URL中寻找JSON文件中的某些字段

BufferedReader reader = new BufferedReader(new InputStreamReader(
     connection.getInputStream())); 
stringV = reader.readLine(); 
//System.out.println(manifest); 
while ((line = reader.readLine()) != null) { 
    stringV = stringV + gson.toJson(line); 
    //manifest = manifest + line; 
} 
System.out.println(manifest); 

manifest man = gson.fromJson(manifest, manifest.class); 
testString = man.getversion(); 
System.out.println(test); 

我的一个问题涉及到我专用于JSON类,我需要有声明,获取/套,所有在JSON中的字段?

public class manifest{ 
    private List<String> versions; 
    private String version; 
    private String current; 
    private String compatible; 
    private String published; 
    private String description; 
    private List<String> manifest; 
    private String jre; 

    public List<String> getVersions(){return versions;} 
    public String getversion(){return version;} 
    public String getcurrent(){return current;} 
    public String getcomp(){return compatible;} 
    public String getpub(){return published;} 
    public String getdesc(){return description;} 
    public List<String> getFileList(){return manifest;} 
    public String getjre(){return jre;} 

    public void setVersions(List<String> versions){this.versions = versions;} 
    public void setversion(String version){this.version = version;} 
    public void setcurrent(String current){this.current = current;} 
    public void setcomp(String compatible){this.compatible = compatible;} 
    public void setpub(String published){this.published = published;} 
    public void setdesc(String description){this.description = description;} 
    public void setFileList(List<String> manifest){this.manifest = manifest;} 
    public void setjre(String jre){this.jre = jre;} 

    @Override 
    public String toString(){ 
     return String.format("Versions:%s,version:%s,curr:%s,comp:%s,pub:%s,desc:%s,manifest:%s,jre:%s", 
       versions, version, current, compatible, published, description, manifest, jre); 
    } 
} 

这里的JSON的结构:

 { 
     "versions": [ 
      { 
     "version": "II V7.1.2", 
     "current": true, 
     "compatible": true, 
     "published": "2012-04-21T18:25:43-05:00", 
     "description": "Stability improvements for users:\n\n-Fix a crash\n-Fix a bug", 
     "manifest": [ 
      { 
       "name": "file.jar", 
       "checksum": "56116165156111156156116161651161616116165116116516651651" 
      }, (more elements here) 
     "jre": "1.7.0_17" 

任何帮助是非常赞赏,因为我不熟悉JSON的。谢谢!

回答

1

JSON实际上很容易在对象世界中概念化。您可以将其视为数据映射,键是字符串,值可以是基本类型(布尔值,数字,字符串,日期),数组或其他映射中的任何一种。每当这个值最终成为另一张地图时,你就会创建一个对象!就这么简单。

所以你的情况,让我们看看JSON段,您包括:

{ 
    "versions": [ 
     { 
      "version": "II V7.1.2", 
      "current": true, 
      "compatible": true, 
      "published": "2012-04-21T18:25:43-05:00", 
      "description": "Stability improvements for users:\n\n-Fix a crash\n-Fix a bug", 
      "manifest": [ 
       { 
        "name": "file.jar", 
        "checksum": "56116165156111156156116161651161616116165116116516651651" 
       } 
      ], 
      "jre": "1.7.0_17" 
     } 
    ] 
} 

在这个片段中,外JSON对象包含了一个JSON元素,这是一个数组,名称versions。这个阵列中的每个元素自然地映射到一个对象,给你一个顶级的定义是这样的:

public class Root { 
    private List<Version> versions; 

    // Constructors, getters, setters 
} 

现在,你就可以开始看Version对象的细节。使用这些规则创建Java对象:

  • 将每个基元类型值映射到等效的Java属性。
  • 将对象类型值映射到类。
  • 地图阵列Java集合类型

日期是不是原始的,但对于测绘的目的,我们将适用同样的规则

public class Version { 
    private String version; 
    private Boolean current; 
    private Boolean compatible; 
    private Date published; 
    private String description; 
    private List<Manifest> manifest; // Note the collection *and* object mapping 
    private String jre; 
    // Constructors, getters, setters 
} 

public class Manifest { 
    private String name; 
    private String checksum; 
} 

很简单吧?那么您会在顶层的JSON,映射到顶级对象,用下面的代码:

Root obj = new Gson().fromJson(jsonStr, Root.class); 

上共有侧面说明,您使用的检索JSON代码中有一些不必要的/不正确代码:

  • 使用StringBuilder建立你的字符串,而不是更昂贵的concat操作
  • 在while循环,调用gson.toJson(line);是不必要的,实际上是不正确。
+0

非常感谢!现在它更有意义。 – 2013-03-19 18:13:45

+0

之后,为了从JSON对象获取特定的信息字段,你会推荐使用解析器吗?还是有更简单的方法? – 2013-03-19 18:18:08

+0

将其转换为Java对象后,您不需要执行任何其他解析,只需访问与其他对象相同的对象属性即可。 – Perception 2013-03-19 18:42:02