从文本文件中读取JSON [英] Reading JSON from a text file

查看:128
本文介绍了从文本文件中读取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一些JSON库,我现在正在研究Google-JSON,但我想要实现的只是简单的事情,我想知道你会建议什么。

I know of some JSON libs around and I'm currently looking into Google-JSON but all I want to achieve is something simple and I want to know what you would suggest.

我想要一个JSON库,让我读取一个JSON文本文件,让我把它转换成字符串,int,boolean等。
- 现在使用Json.org/java

I want a JSON library which will let me read a textfile that is in JSON and let me convert it into strings, int, boolean, etc. -- Now using Json.org/java

它可以阅读!但是!!

It can READ! BUT!!

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]);

    }
}

编译时我收到此错误:

When compiling I get this error:

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


推荐答案

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

Install Google Gson and create those two model classes

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:

And convert your JSON as follows:

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

要获得标题,请执行以下操作:

To get the title just do:

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

并获取x = 3和y = 3的地图项目:

And to get the map item at x=3 and y=3:

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

并获取第一个项目的名称

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

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

Easy! Converting the other way on is also simple using Gson#toJson().

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

参见这个答案另一个复杂的Gson例子。

See also this answer for another complex Gson example.

这篇关于从文本文件中读取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆