Json auslesen

MrKavatch

Mitglied
Hey,
ich wollte mal kurz fragen wie es den möglich ist von einer Json datei die daten auszulesen.
Als beispiel ich habe diese Datei :
Code:
[{"map":{"infos":{"name":"Wald","version":"1.0","authores":"MrKavatch / MisterQuatsch"},"teams":{"gelb":{"spawn":{"x":"-40","y":"81","z":"-208"},"protection":{"center":{"x":"-16","z":"-213"},"radius":"35"}},"grün":{"spawn":{"x":"266","y":"64","z":"-510"},"protection":{"center":{"x":"264","z":"-495"},"radius":"30"}}},"kit":{"slot":{"slot1":{"item":"WOOD_SWORD","ammount":"1","meta":"0"},"slot2":{"item":"STONE_PICKAXE","ammount":"1","meta":"0"},"slot3":{"item":"APPLE","ammount":"16","meta":"0"},"slot4":{"item":"LOG","ammount":"32","meta":"0"},"slot5":{"item":"AIR","ammount":"1","meta":"0"},"slot6":{"item":"AIR","ammount":"1","meta":"0"},"slot7":{"item":"AIR","ammount":"1","meta":"0"},"slot8":{"item":"AIR","ammount":"1","meta":"0"},"slot9":{"item":"AIR","ammount":"1","meta":"0"},"helmet":{"item":"AIR","ammount":"1","meta":"0"},"chestplate":{"item":"LEATHER_CHESTPLATE","ammount":"1","meta":"0"},"leggings":{"item":"LEATHER_LEGGINGS","ammount":"1","meta":"0"},"boots":{"item":"LEATHER_BOOTS","ammount":"1","meta":"0"}},"extras":{"effects":{"effect1":{"type":"DAMAGE_RESISTANCE","lenght":"25","strength":"200"},"effect2":{"type":"BLINDNESS","lenght":"2","strength":"10"},"effect3":{"type":"SATURATION","lenght":"100","strength":"10"}}}},"filer":{"blocks":{"noPlace":{"errorMsg":"§cDu darfst diesen Block nicht Setzen!"},"noBreak":{"errorMsg":"§cDu darfst diesen Block nicht Zerstören!"}}}}}]
Wie könnte ich dabei jetzt zb die versions nummer abrufen?

// Edit soweit hab ich es schon:
Code:
public class Json {
    private final static JSONParser jsonParser = new JSONParser();
    static JSONArray array;
    public static void main(String[] args) throws IOException, ParseException {
        URL oracle = new URL("http://www.greev.eu/Json/map");
        BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            array = (JSONArray) jsonParser.parse(inputLine);
           
            System.out.println(inputLine);
        }
        in.close();

            JSONObject jsonProfile = (JSONObject) array.get(0);;
            String name = (String) jsonProfile.get("version");
            System.out.println("NAME >>> " + name);
    }
}
 
Du könntest erwähnen, welchen JSONParser Du verwendet.
Und was Dein Problem ist. (Welche Ausgabe erwartest Du, und was wird ausgegeben?)
Zudem könntest Du kompilierbaren Code zur Verfügung stellen. (inkl. imports...)

Das alles erhöht Deine Chance eine gute und schnelle Antwort zu bekommen.
 
Ok der Komplette Code:
Code:
package main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Json {
    private final static JSONParser jsonParser = new JSONParser();
    static JSONArray array;
    public static void main(String[] args) throws IOException, ParseException {
        URL oracle = new URL("http://www.greev.eu/Json/map");
        BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            array = (JSONArray) jsonParser.parse(inputLine);
           
            System.out.println(inputLine);
        }
        in.close();

            JSONObject jsonProfile = (JSONObject) array.get(0);;
            String name = (String) jsonProfile.get("version");
            System.out.println("NAME >>> " + name);
    }
}
Parser: Json-Simple
Und es soll mir einfach die 'Version' von dem Json code der oben schon steht ausgeben.
Was aber ausgegebn wird ist 'null'
 
Dein JSON-Objekt ist verschachtelt. Die Versionsnummer ist ja Bestandteil von "infos", dieses ist wieder Bestandteil von "map". Bei mir klappt's so:
Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Json {
    private final static JSONParser jsonParser = new JSONParser();
    static JSONArray array;
    public static void main(String[] args) throws IOException, ParseException {
        URL oracle = new URL("http://www.greev.eu/Json/map");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                oracle.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            array = (JSONArray) jsonParser.parse(inputLine);
            System.out.println(inputLine);
        }
        in.close();

        JSONObject jsonProfile = (JSONObject) array.get(0);

        JSONObject map = (JSONObject) jsonProfile.get("map");
        JSONObject infos = (JSONObject) map.get("infos");
        String versionID = (String) infos.get("version");
       
        System.out.println("versionID >>> " + versionID);
    }
}
 
Dein JSON-Objekt ist verschachtelt. Die Versionsnummer ist ja Bestandteil von "infos", dieses ist wieder Bestandteil von "map". Bei mir klappt's so:
Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Json {
    private final static JSONParser jsonParser = new JSONParser();
    static JSONArray array;
    public static void main(String[] args) throws IOException, ParseException {
        URL oracle = new URL("http://www.greev.eu/Json/map");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                oracle.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            array = (JSONArray) jsonParser.parse(inputLine);
            System.out.println(inputLine);
        }
        in.close();

        JSONObject jsonProfile = (JSONObject) array.get(0);

        JSONObject map = (JSONObject) jsonProfile.get("map");
        JSONObject infos = (JSONObject) map.get("infos");
        String versionID = (String) infos.get("version");
      
        System.out.println("versionID >>> " + versionID);
    }
}
Vielen danke schonmal dafür! Ich denke mal das es so funktionieren könnt, aber gibt es vlt sogar einen kürzere version als das? Ich meine das ist ja schon etwas länge.
Also ich meine sowas wie
Code:
jsonProfile.get("map.infos.version");
vlt kennst du da ja was. Es kann natürlich auch sein das ich damit total falsch bin ^^
Mfg
 

Zurück
Oben