Solved JSON help

Discussion in 'Plugin Development' started by Funergy, Nov 22, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Funergy

    So I've queried a server and now I want to parse it into a java object
    this is what my JSON line looks like:

    { "_id" : { "$oid" : "547084b10ede9afb9ef5fa40"} , "UUID" : "INSERT UUID HERE" , "rank" : "Owner" , "coins" : 10 , "friends" : [ "UUID" , "UUID"]}

    but I want to convert it into
    String s = The UUID;
    String r = the rank;
    Integer c = coins;
    ArrayList<String> friends = ArrayList<String>();
    and then get all the friends and put them to the arraylist.

    So what I'm asking how to get parts of the line into a java line
     
  2. Funergy
    Have you tried searching at all?
     
  3. Offline

    Funergy

    Assist Yes of course.
    What I googled
    "Parse JSON to Java object". but I don't really understand/know how to do it.
    This is the first time I start learning JSON

    I've even looked how the UUID fetcher worked
    but then I've got this error

    java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray
     
  4. Offline

    Funergy

    Assist I've already seen that one but
    JSONObject obj =newJSONObject(" .... ");
    Doesn't let me use the "....."
    It says JSONObject obj =newJSONObject(" .... "); needs to be like this
    JSONObject obj =newJSONObject();
     
  5. Offline

    Funergy

    Assist No the built in library in CraftBukkit
     
  6. Funergy
    I don't know which one Craftbukkit is using, but they should all work pretty much the same. Give me a moment, I'll take a look.

    Edit: I don't think Craftbukkit is even using any special JSON library. You're probably talking about the default json library org.json.simple. You should download the library I linked earlier.
     
  7. Offline

    Funergy

    Assist Where is the download link? I can't find it
    and if I downloaded it then, do I need to use MANIFEST.MF to use it again?
     
  8. Offline

    Cybermaxke

    You need to use the json parser:
    Code:
    String jsonString;
     
    JSONParser parser = new JSONParser();
    JSONObject object = (JSONObject) parser.parse(jsonString);
    And you do not need to include the library because it's already included in the bukkit api.
     
  9. Offline

    Funergy

    Cybermaxke TY.
    but I have an array in the JSON line how should I put them into a HashSet/ArrayList

    ["Funergy","DubstepPrins"]

    nvm Solved :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
Thread Status:
Not open for further replies.

Share This Page