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) {



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
+3  A: 

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

So I download JSONObject.java and import it. Then I make that...? No examples or nothing sheesh .:S
Yeah, now i'm stuck. :\
Check the question at top. I edited it :)
Yeah, it's my JSON. Some syntax inside it is incorrect hence the error.

json-lib comes with an example of converting a String to a JSON Object:


+1  A: 

The Spring Framework uses Jackson, so that's a fairly good endorsement for Jackson.


See the "Simple Data Binding Example" heading if you just want to use generic Maps.

Paul Grime

You can do this just fine with google-gson. I think it'd look something like this:

JsonParser parser = new JsonParser();
JsonObject object = parser.parse(text).getAsJsonObject();

String title = object.get("title").getAsString();
int currentMap = object.get("currentMap").getAsInt();
I just tried that and this error: http://www.googlerrror.pastebin.com/8FFvmTCWI think my JSON is messed up. It's me, not the libs... :\
+1  A: 

As for the error messages.

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

You don't usually name your package the same way the package you want to import, although you can.

You have to either: 1 name it different, 2.- don't put the import

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

You're missing a "new" there... it should be new JSONObject(...

Check the question at top. I edited it :)
@Dan, ok, I added a new answer.
I added a new error/question.
+1  A: 

org.json.JSONException: Expected a ':' after a key at 148 [character 149 line 1]

Here, your json string is invalid:

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

That creates and array with objects inside, the first object has the attributes 1,3,1 etc without value.

Should be:

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

For that to be an array with arrays inside.


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

So you can have attributes 1,3,1 etc with value 0 but... that doesn't make sense

OK, I got it all sorted out. Now I need to know how map array works cause it's buggin' out for me!
+2  A: 

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

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.

I get errors when I follow this. :\
Errors tell something about the cause of the problem. Ignoring errors doesn't help us to help you explaining them. You know, once the cause of the problem is *understood*, the solution is nothing than obvious. So, share them with us :)
It's all here... http://www.googlejsonerror.pastebin.com/N5iDazz8error + source - what am i doing wrong?
The JSON key `map` is not properly quoted. You forgot a singlequote in front of `map`.
Nice catch! Now I get this http://www.googlejsonerror.pastebin.com/6GCEunXW
Those inner classes are not immediately constructable. Either declare those classes `static`, or put them without `public` modifier at bottom below the current class (i.e. do not nest them!), or just put them standalone in a same package.
Worked! :-D Thanks. Google Json is go for meeeeeeeeee
Wait... awwC:\Users\Dan\Documents\gJson\readGoogle.java:37: 'void' type not allowed here System.out.println(System.out.println(data.getMap()[3][3])); // Map One:(
`println()` indeed returns nothing (`void`) Reread that line once again, you impatient! ;)
Ahhhhh whoops. Another mistake. Oooooooooooops. :D Thanks. Yes, I am impatient! :D
Wait I'm having trouble using the getItem() method.... help?
It's called `getItems()` and it returns a `List<Item>`.
Well, I must have never seen this List<Item> before because it's weird. I tried using data.getItems() but no success... honestly... I fail.
I added an example in my answer. Again, without errors we can't help you more in detail. Errors tell something about the cause of the problem.
Oh I didn't see the item one. Thanks.
`System.out.println(data.getItems().get(0).getName()); // Pickaxe` returns null o_O
Sorry, can't reproduce it on the posted json (with the quote before `map` fixed). Your problem lies somewhere else.