繁体   English   中英

JSON + Java入门困难

[英]Trouble getting started with JSON + Java

我是JSON的新手,阅读了一些教程,在API中进行了搜索,并搜索了相关文章。 我已经将javax.json-1.0.4 JAR添加到了我的项目( http://mvnrepository.com/artifact/org.glassfish/javax.json/1.0.4 )。 我需要从将在算法中使用的JSON文件中检索一些信息。 样本JSON文件的摘录:

"topology": "sndlib_raw:/sndlib_atlanta", 
"server_kind": "uniform", 
"success": 1, 
"algorithm": "BachelorthesisCR", 
"spread_factor": Infinity, 
"r_client": "{0: 12, 1: 1, 2: 0}", 
"tried_scalings": NaN, 
"service_rate": NaN, 
"jobid": "00a77fbd-1167-4843-bd3e-7754125fc173", 
"templateshort": "Video", 
"graph": {
    "nodes": {
        "0": {
            "geolocation": [
                -0.20689655172414234, 
                -2.3478260869565304
            ], 
            "name": "N1", 
            "kcost": 0.0, 
            "k": 6, 
            "plot_coord": [
                177.65217391304347, 
                89.793103448275858
            ], 
            "ar": 0, 
            "sr": 1.0, 
            "users": 0
        }, 
        "1": {
            "geolocation": [
                -19.655172413793096, 
                129.13043478260869

...

"edges": {
        "0,7": {
            "lat": 0.0, 
            "dr": 30.0, 
            "geodistance": 8882.5879243898
        }, 
        "0,6": {
            "lat": 0.0, 
            "dr": 30.0, 
            "geodistance": 8756.266243327267
        }, 

文件中的大多数信息对我来说都是无关紧要的。 我只对“ r_client”,节点以及它们的一些信息以及其他一些东西感兴趣。 因此,我认为使用JSON对象模型API而不是流API可能会更容易。

在测试程序中,我想读取并打印节点名称:

public JSONReader (String input) throws FileNotFoundException {
    inputFile = input;
    reader = Json.createReader(new FileReader(inputFile));
    jsonst = reader.read(); 
}

public void read () {
    JsonObject obj = reader.readObject();
    JsonArray results = obj.getJsonArray("nodes");
    int i = 0;
    String nodeName = "" + i;
    for (JsonObject result : results.getValuesAs(JsonObject.class)) {
        System.out.println(result.getJsonObject(nodeName).getString("name"));
        i++;
        nodeName = "" + i;
    }
}

但是,我不确定何时使用JsonObject和何时使用JsonArray。 如果我宁愿使用JsonReader还是JsonParser? 我不需要更改JSON文件。 当我启动程序时,我收到“ JsonParsingException:意外的字符78”。 根据现有的帖子,这是由于未引用的东西,例如“ tried_scalings”:NaN,其中NaN未引用。 我尝试了JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES; 就像某处建议的那样,但是没有用。 还是只有杰克逊才能做到(有什么区别?)?

如何在不知道其名称的情况下遍历“ edge”-子树的所有节点?

您的JSON不是有效的JSON-Infinity和NaN不是JSON中的关键字(它不是JavaScript)。 通过像样的短绒棉,例如使用http://jsonlint.com

如果您想要更轻松的处理(而且非常快),请选择杰克逊。

Maven:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.3</version>
</dependency>

Java:

public static void main (String [] args) throws IOException
{
  ObjectMapper mapper = new ObjectMapper (); // can reuse, share globally
  mapper.configure (Feature.ALLOW_NON_NUMERIC_NUMBERS, true);
  JsonNode tree = mapper.readTree (new File ("foo.json"));
  // assuming "edges" is a property of the root object
  final JsonNode edges = tree.get ("edges");
  for (JsonNode edge : edges)
  {
    final double lat = edge.get ("lat").asDouble ();
    final double dr = edge.get ("dr").asDouble ();
    final double geodistance = edge.get ("geodistance").asDouble ();
  }
}

至于您以后关于如何遍历JsonNode -来吧,所有这些都在JavaDoc中 JsonNode实现了Iterable<JsonNode> ,它在进行迭代时非常简单...

暂无
暂无

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

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