Lesen JSON aus einer text-Datei

Ich kenne einige JSON-libs herum und ich bin derzeit auf der Suche in Google-JSON-aber alle, die ich erreichen möchte, ist etwas zu einfach und ich will wissen, was Sie vorschlagen würde.

Ich möchte eine JSON-Bibliothek, die ich lese eine Textdatei, die in JSON und lassen Sie mich es zu konvertieren in strings, int, boolean, etc.
-- Jetzt mit Json.org/java

Kann es LESEN! ABER!!

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

    }
}

Beim kompilieren bekomme ich diesen Fehler:

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
  • Wahrscheinlich öffnen Sie eine neue Frage für jedes problem, das Sie hatten, war in Ordnung. Sie begannen zu Fragen, eine Sache, und am Ende fragt eine andere.
InformationsquelleAutor nn2 | 2010-07-19
Schreibe einen Kommentar