[Help Me] Get part of JSON string easy

Discussion in 'Spigot Plugin Development' started by nfell2009, May 25, 2015.

  1. Heyyyyy,

    So I'm trying to use JSON (and failing), so I'd like to know how I can easily get a string from JSON.

    For example if I get the JSON from: http://status.mojang.com/check, how do I get "minecraft.net"s value easily? I know PHP has a very simply way, so I'm wondering if Java has the same.

    Any and all help is amazing <3
    Thankies
     
  2. Use a JSON library like GSON.
     
  3. Thanks :) I was trying with org.json, but when using JSONObject, I get it must start with "{" which Mojang's status check does not, and then with JSONArray, I get 0 is not a string ;-;
     
  4. Can I see what you've tried?
     
  5. "in" is text from http://status.mojang.com/check - while "s" is "minecraft.net".

    Code (Java):

    JSONObject json = new JSONObject(in);
    String out = json.getString(s);
    System.out.println(out);
     
    Code (Java):

    JSONArray json = new JSONArray(in);
    String out = json.getString(s);
    System.out.println(out);
    [CODE]
     
  6. making that json parser for umbaska eh ;)?
     
  7. Maybe :p Just got no idea wtf I'm doing xD I prefer PHP <3
     
  8. Haha. It's a JSON array you are trying to parse right now. Grab the JSONObject from the array then grab the string.
     
  9. I literally have no idea anymore ._.
    Code (Java):

    JSONArray json = new JSONArray(in);
    JSONObject jsonO = new JSONObject(json.getString(s));
    System.out.println(jsonO.toString());
     
    I tried it and it doesn't work :'( Sorry to be an absolute pain xD I'm guessing it's probably something really simply that I'm just not getting :p
     
  10. Code (Text):
    URL url = new URL("http://status.mojang.com/check");

    Scanner scanner = new Scanner(url.openStream());
    scanner.close();

    JSONArray array = new JSONArray(scanner.next());
    JSONObject object = array.getJSONObject(0);
    String result = object.getString("minecraft.net");
    System.out.print(result);
    You are welcome.
     
    • Like Like x 2
    • Useful Useful x 1
  11. That is amazing <3 Thankies