Java:从http url获取json(使用json.org api)
我当前正在尝试用java()接收此url 问题是,我可以完美地读取任何以.json结尾的文件,但由于.php(我认为)的缘故,它对这个文件不起作用。 另外,如果有人能告诉我如何将item_name之类的东西放入我可以使用的变量中?那太好了 我的代码:Java:从http url获取json(使用json.org api),java,php,json,api,Java,Php,Json,Api,我当前正在尝试用java()接收此url 问题是,我可以完美地读取任何以.json结尾的文件,但由于.php(我认为)的缘故,它对这个文件不起作用。 另外,如果有人能告诉我如何将item_name之类的东西放入我可以使用的变量中?那太好了 我的代码: URL u; try { u = new URL("https://api.wynncraft.com/public_api.php?action=items&command=75");
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();
}
}
这有点神秘,你应该能够确定变量值。
如果使用PHP,JSON的结构就是这样的:
$json = json_decode($data,true);
k1,k2表示结构中的级别。-------------------------------
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
)
#################################################################
具有输出类型的数组构造摘要
#################################################################
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
import org.json.*;
JSONParser JSON = new JSONParser();
JSONObject obj = JSON.parse(line);
String hr0 = obj.getJSONObject("0")
.getJSONObject("Identification")
.getString("health_regen");
究竟是什么问题?