Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading JSON from a text file

Tags:

java

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.

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
like image 746
test Avatar asked Jul 19 '10 22:07

test


2 Answers

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; }
}

and

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; }
}

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

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

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

And to get the name of the first Item:

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

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

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

See also this answer for another complex Gson example.

like image 92
BalusC Avatar answered Oct 10 '22 12:10

BalusC


I recommend this library: http://www.json.org/java/

You simply have to create a JSONObject from string, and get the name proprety.

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

Download the source, and import it into your project: http://www.json.org/java/json.zip

like image 25
Nican Avatar answered Oct 10 '22 11:10

Nican