【问题标题】:Reading JSON from a text file从文本文件中读取 JSON
【发布时间】:2010-07-19 22:12:29
【问题描述】:

我知道一些 JSON 库,我目前正在研究 Google-JSON,但我想要实现的只是一些简单的东西,我想知道你的建议。

我想要一个 JSON 库,它可以让我读取 JSON 格式的文本文件并将其转换为字符串、int、布尔值等。 -- 现在使用 Json.org/java

它可以阅读!但是!!

import org.json.*;

public class readJ {

    public static String MapTitle;
    public static int[][] tiles;

    public static void main(String[] args) {

                       String json =
               "{"
               +"'name': 'map_one.txt',"
                +"'title': 'Map One',"
                +"'currentMap': 4,"
                +"'items': ["
                     +"{ name: 'Pickaxe', x: 5, y: 1 },"
                     +"{ name: 'Battleaxe', x: 2, y: 3 }"
                     +"],"
                +"map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
                    +"[ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
                    +"[ 1,7,1,1,1,24,1,1,24,1,1,1,1 ],"
                    +"[ 1,7,1,1,7,1,1,1,24,1,1,1,1 ],"
                    +"[ 1,7,7,7,1,24,24,24,24,1,1,1,1 ],"
                    +"[ 1,1,7,1,1,24,1,24,1,1,1,1,1 ],"
                    +"[ 1,1,1,1,1,24,1,1,1,1,1,1,1 ],"
                    +"[ 1,1,3,1,1,24,1,1,1,1,1,1,1 ],"
                    +"[ 1,3,3,1,1,24,1,1,1,1,1,1,1 ]]"
+"}";
try {
JSONObject JsonObj = new JSONObject(json);
MapTitle = JsonObj.getString("title");
tiles = JsonObj.getJSONArray("map");
}catch (JSONException er) {
    er.printStackTrace();
}

System.out.println(MapTitle);
System.out.println(tiles[0][1]);

    }
}

编译时出现此错误:

C:\Users\Dan\Documents\readJSON\readJ.java:32: incompatible types
found   : org.json.JSONArray
required: int[][]
tiles = JsonObj.getJSONArray("map");
                            ^
1 error

Tool completed with exit code 1

【问题讨论】:

  • 可能会为您遇到的每个问题打开一个新问题。你开始问一件事,最后问另一件事。

标签: java json


【解决方案1】:

安装Google Gson 并创建这两个模型类

public class Data {
    private String name;
    private String title;
    private int currentMap;
    private List<Item> items;
    private int[][] map;

    public String getName() { return name; }
    public String getTitle() { return title; }
    public int getCurrentMap() { return currentMap; }
    public List<Item> getItems() { return items; }
    public int[][] getMap() { return map; }

    public void setName(String name) { this.name = name; }
    public void setTitle(String title) { this.title = title; }
    public void setCurrentMap(int currentMap) { this.currentMap = currentMap; }
    public void setItems(List<Item> items) { this.items = items; }
    public void setMap(int[][] map) { this.map = map; }
}

public class Item {
    private String name;
    private int x;
    private int y;

    public String getName() { return name; }
    public int getX() { return x; }
    public int getY() { return y; }

    public void setName(String name) { this.name = name; }
    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
}

并按如下方式转换您的 JSON:

Data data = new Gson().fromJson(json, Data.class);

要获得标题,只需:

System.out.println(data.getTitle()); // Map One

并在 x=3 和 y=3 处获取地图项:

System.out.println(data.getMap()[3][3]); // 1

并获取第一个Item的名字:

System.out.println(data.getItems().get(0).getName()); // Pickaxe

简单!使用Gson#toJson() 转换另一种方式也很简单。

String json = new Gson().toJson(data);

另请参阅this answer 了解另一个复杂的 Gson 示例。

【讨论】:

  • 错误说明了问题的原因。忽略错误并不能帮助我们帮助您解释它们。要知道,一旦了解了问题的原因,解决方案就显而易见了。所以,与我们分享它们:)
  • 都在这里...googlejsonerror.pastebin.com/N5iDazz8错误+来源-我做错了什么?
  • JSON 键 map 未正确引用。你忘记了map前面的单引号。
  • 那些内部类不能立即构造。要么声明这些类static,要么将它们不带public修饰符放在当前类的底部(即不要嵌套它们!),或者将它们单独放在同一个包中。
  • 成功了! :-D 谢谢。 Google Json 适合 meeeeeeeeee
【解决方案2】:

我推荐这个库: http://www.json.org/java/

您只需从字符串创建一个 JSONObject,并获取名称属性。

JSONObject JsonObj = JSONObject( InputStr );
String MapTitle = JsonObj.getString("title");

下载源代码,并将其导入您的项目:http://www.json.org/java/json.zip

【讨论】:

  • 所以我下载了 JSONObject.java 并导入它。那我做那个...?没有例子或什么都没有。:S
  • 是的,这是我的 JSON。它里面的一些语法不正确,因此出现错误。
  • 下载链接已损坏。请转至此处:github.com/douglascrockford/JSON-java/zipball/master
【解决方案3】:

Spring 框架使用 Jackson,因此这对 Jackson 来说是一个相当不错的认可。

JacksonInFiveMinutes

如果您只想使用通用地图,请参阅“简单数据绑定示例”标题。

【讨论】:

    【解决方案4】:

    您可以使用 google-gson 很好地做到这一点。我认为它看起来像这样:

    JsonParser parser = new JsonParser();
    JsonObject object = parser.parse(text).getAsJsonObject();
    
    String title = object.get("title").getAsString();
    int currentMap = object.get("currentMap").getAsInt();
    ...
    

    【讨论】:

    【解决方案5】:

    至于错误信息。

    C:\Users\Dan\Documents\readJSON\readJ.java:2: cannot find symbol
    symbol  : class json
    location: package org
    import org.json;
          ^
    

    您通常不会以与要导入的包相同的方式命名您的包,尽管您可以。

    您必须:1 命名不同,2.- 不要将导入

    C:\Users\Dan\Documents\readJSON\readJ.java:27: cannot find symbol
    symbol  : method JSONObject(java.lang.String)
    location: class org.json.readJ
    JSONObject JsonObj = JSONObject(json);
    

    你错过了一个“新”...应该是new JSONObject(...

    【讨论】:

    • @Dan,好的,我添加了一个新答案。
    【解决方案6】:

    org.json.JSONException: 在 148 [字符 149 第 1 行] 处的键后应有一个“:”

    这里,你的 json 字符串无效:

     + "'map': [ { 1,3,1,1,1,24,1,1,1,1,1,1,1 },"
    

    里面有对象的创建和数组,第一个对象的属性1,3,1等没有值。

    应该是:

     + "'map': [ [ 1,3,1,1,1,24,1,1,1,1,1,1,1 ],"
    

    为了它是一个内部有数组的数组。

    或者

    + "'map': [ { 1:0,3:0,1:0,1:... 
    

    所以你可以拥有值为 0 的属性 1、3、1 等,但是......这没有意义

    【讨论】:

    • 好的,我把一切都整理好了。现在我需要知道 map 数组是如何工作的,因为它对我来说很麻烦!
    【解决方案7】:

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多