2

Im currently trying to receive this url in java (https://api.wynncraft.com/public_api.php?action=items&command=75)

The problem is, I can read any file ending in .json perfectly, but because of the .php (I think) it isnt working for this one. Also, if someone could tell me how to get things like item_name into variables I can use? Would be great...

My code:

URL u;
        try {
            u = new URL("https://api.wynncraft.com/public_api.php?action=items&command=75");

            URLConnection c = u.openConnection();
            InputStream r = c.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(r));
            for (String line; (line = reader.readLine()) != null;)
                System.out.println(line);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
1
  • What exactly is the issue? Commented May 1, 2015 at 15:41

2 Answers 2

1
  1. Download the json.api JAR from http://mvnrepository.com/artifact/org.json/json (or use Maven)

  2. Change your code to read the entire string, not just a line.

  3. Then do something like this to get at the values you want.

    import org.json.*;
    
    JSONParser JSON = new JSONParser();
    JSONObject obj = JSON.parse(line);
    
    String hr0 = obj.getJSONObject("0")
            .getJSONObject("Identification")
            .getString("health_regen");
    
Sign up to request clarification or add additional context in comments.

Comments

0

This is a little cryptic, buy you should be able ascertain the variable values.

This is how the JSON would be structured if you were to use PHP:

$json = json_decode($data,true); 

k1, k2 denote the level with in the structure.

-------------------------------
k0 request => array( 
  k1: timestamp => integer:  $json['request']['timestamp'] => 1430494697
  k1: ip => string:  $json['request']['ip'] => 108.162.210.100
)
-------------------------------
k0 0 => array( 
  k1: item_name => string:  $json[0]['item_name'] => Bob's Mythic Bow
  k1: item_type => string:  $json[0]['item_type'] => Legendary
  k1: item_minecraft => string:  $json[0]['item_minecraft'] => Bow
  k1: item_min_lvl => string:  $json[0]['item_min_lvl'] => 75
  k1 identification => array(
    k2: health_regen => string:  $json[0]['identification']['health_regen'] => 1.5
    k2: mana_regen => string:  $json[0]['identification']['mana_regen'] => 1
    k2: spell_dam => string:  $json[0]['identification']['spell_dam'] => 30%
    k2: life_steal => string:  $json[0]['identification']['life_steal'] => 0.4
    k2: mana_steal => string:  $json[0]['identification']['mana_steal'] => 1
    k2: xp_bonus => string:  $json[0]['identification']['xp_bonus'] => 20%
    k2: loot_bonus => string:  $json[0]['identification']['loot_bonus'] => 20%
  )
  k1: min_dam => string:  $json[0]['min_dam'] => 566
  k1: max_dam => string:  $json[0]['max_dam'] => 463
)
-------------------------------
k0 1 => array( 
  k1: item_name => string:  $json[1]['item_name'] => Bob's Mythic Daggers
  k1: item_type => string:  $json[1]['item_type'] => Legendary
  k1: item_minecraft => string:  $json[1]['item_minecraft'] => Shears
  k1: item_min_lvl => string:  $json[1]['item_min_lvl'] => 75
  k1 identification => array(
    k2: health_regen => string:  $json[1]['identification']['health_regen'] => 1.5
    k2: mana_regen => string:  $json[1]['identification']['mana_regen'] => 1
    k2: spell_dam => string:  $json[1]['identification']['spell_dam'] => 30%
    k2: life_steal => string:  $json[1]['identification']['life_steal'] => 0.4
    k2: mana_steal => string:  $json[1]['identification']['mana_steal'] => 1
    k2: xp_bonus => string:  $json[1]['identification']['xp_bonus'] => 20%
    k2: loot_bonus => string:  $json[1]['identification']['loot_bonus'] => 20%
  )
  k1: min_dam => string:  $json[1]['min_dam'] => 420
  k1: max_dam => string:  $json[1]['max_dam'] => 389
)
-------------------------------
k0 2 => array( 
  k1: item_name => string:  $json[2]['item_name'] => Bob's Mythic Spear
  k1: item_type => string:  $json[2]['item_type'] => Legendary
  k1: item_minecraft => string:  $json[2]['item_minecraft'] => Shovel
  k1: item_min_lvl => string:  $json[2]['item_min_lvl'] => 75
  k1 identification => array(
    k2: health_regen => string:  $json[2]['identification']['health_regen'] => 1.5
    k2: mana_regen => string:  $json[2]['identification']['mana_regen'] => 1
    k2: spell_dam => string:  $json[2]['identification']['spell_dam'] => 30%
    k2: life_steal => string:  $json[2]['identification']['life_steal'] => 0.4
    k2: mana_steal => string:  $json[2]['identification']['mana_steal'] => 1
    k2: xp_bonus => string:  $json[2]['identification']['xp_bonus'] => 20%
    k2: loot_bonus => string:  $json[2]['identification']['loot_bonus'] => 20%
  )
  k1: min_dam => string:  $json[2]['min_dam'] => 368
  k1: max_dam => string:  $json[2]['max_dam'] => 311
)
-------------------------------
k0 3 => array( 
  k1: item_name => string:  $json[3]['item_name'] => Bob's Mythic Wand
  k1: item_type => string:  $json[3]['item_type'] => Legendary
  k1: item_minecraft => string:  $json[3]['item_minecraft'] => Stick
  k1: item_min_lvl => string:  $json[3]['item_min_lvl'] => 75
  k1 identification => array(
    k2: health_regen => string:  $json[3]['identification']['health_regen'] => 1.5
    k2: mana_regen => string:  $json[3]['identification']['mana_regen'] => 1
    k2: spell_dam => string:  $json[3]['identification']['spell_dam'] => 30%
    k2: life_steal => string:  $json[3]['identification']['life_steal'] => 0.4
    k2: mana_steal => string:  $json[3]['identification']['mana_steal'] => 1
    k2: xp_bonus => string:  $json[3]['identification']['xp_bonus'] => 20%
    k2: loot_bonus => string:  $json[3]['identification']['loot_bonus'] => 20%
  )
  k1: min_dam => string:  $json[3]['min_dam'] => 278
  k1: max_dam => string:  $json[3]['max_dam'] => 204
)

#################################################################

Summary of Array Constructs With Output Type

#################################################################

1 $json[int]['identification']['health_regen'] string
1 $json[int]['identification']['mana_regen'] string
1 $json[int]['identification']['spell_dam'] string
1 $json[int]['identification']['life_steal'] string
1 $json[int]['identification']['mana_steal'] string
1 $json[int]['identification']['xp_bonus'] string
1 $json[int]['identification']['loot_bonus'] string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.