线程“main”中的异常org.json.JSONException:找不到JSONObject [“pod”]

时间:2016-04-10 14:31:30

标签: java json parsing

我正在尝试从xml转换的json字符串中检索信息。我要求来自wolfram alpha服务器的关于“乌鸦鸟”的信息,我想从json字符串获取最大记录的寿命,这是“alt”中的信息。寿命似乎在一个名为“supbpod”的数组中。完整的json String很长,所以这是我试图从

中检索信息的部分
 {"scanner":"Species","subpod":[{"img":{"src":"http://www4b.wolframalpha.com/Calculate/MSP/MSP8471i7019ibc573d0d6000061c3540eaifae721?MSPStoreType=image/gif&s=55","alt":"maximum recorded lifespan | 69 years  (in captivity)weight | 2.3 lb  (pounds)maximum recorded wingspan | 4.1 feet","width":391,"title":"maximum recorded lifespan | 69 years  (in captivity)weight | 2.3 lb  (pounds)maximum recorded wingspan | 4.1 feet","height":100},"plaintext":"maximum recorded lifespan | 69 years  (in captivity)weight | 2.3 lb  (pounds)maximum recorded wingspan | 4.1 feet","title":"Basic properties"}

这是我的代码

import java.net.*;
import java.io.*;
import org.json.*;
import java.net.URLConnection;
import java.util.Scanner;
import java.net.URL;
import javax.swing.*;


public class Test {

public static void main(String[] args)throws IOException, JSONException{//Beginning of class
    // TODO Auto-generated method stub
    String appID = "";
    String search = "ravenbird";

    URL wolframData = new URL("http://api.wolframalpha.com/v2/query?input="+search+"&appid="+appID);

    URLConnection connection = wolframData.openConnection();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    //reads in the data
    StringBuilder str = new StringBuilder();
    String xmlDoc;

    while((xmlDoc = in.readLine()) != null){ //converts buffer reader to string
    str.append(xmlDoc);
    }
    in.close();

   JSONObject wolframJson = (JSONObject) XML.toJSONObject(str.toString());

    System.out.println(wolframJson.toString());//test

    try{
    JSONArray arr = wolframJson.getJSONArray("subpod");

    String lifespan = "";

    for(int i = 0; i < arr.length(); i++)
    {
        lifespan = arr.getJSONObject(i).getString("alt");
    }
    }
    catch(JSONException je)
    {
        System.out.println(je);
    }





}//End of method

}//End of class

由于我有一个try和catch语句,打印出的消息是:

 org.json.JSONException: JSONObject["subpod"] not found.

以下是完整的json字符串:

 {"queryresult":{"server":23,"timedout":"Data,Character","pod":[{"scanner":"Identity","subpod":{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26821f9485ga5452748d00004353ab5216bid777?MSPStoreType=image/gif&s=23","alt":"raven  (animal)","width":102,"title":"raven  (animal)","height":18},"plaintext":"raven  (animal)","title":""},"numsubpods":1,"id":"Input","position":100,"title":"Input interpretation","error":false},{"scanner":"Data","subpod":{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26831f9485ga5452748d000068f0hb74dh0ibg78?MSPStoreType=image/gif&s=23","alt":"Corvus corax","width":83,"title":"Corvus corax","height":18},"plaintext":"Corvus corax","title":""},"numsubpods":1,"id":"ScientificName:SpeciesData","position":200,"title":"Scientific name","error":false},{"scanner":"Data","subpod":{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26841f9485ga5452748d00003f2a2979i5bf7079?MSPStoreType=image/gif&s=23","alt":"kingdom | Animalia  (animals)phylum | Chordata  (chordates)class | Aves  (birds)order | Passeriformes  (perching birds)family | Corvidaegenus | Corvusspecies | Corvus corax  (raven)","width":310,"title":"kingdom | Animalia  (animals)phylum | Chordata  (chordates)class | Aves  (birds)order | Passeriformes  (perching birds)family | Corvidaegenus | Corvusspecies | Corvus corax  (raven)","height":228},"plaintext":"kingdom | Animalia  (animals)phylum | Chordata  (chordates)class | Aves  (birds)order | Passeriformes  (perching birds)family | Corvidaegenus | Corvusspecies | Corvus corax  (raven)","title":""},"numsubpods":1,"id":"Taxonomy:SpeciesData","position":400,"title":"Taxonomy","error":false},{"scanner":"Species","subpod":[{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26851f9485ga5452748d00004idia801fbf2gc35?MSPStoreType=image/gif&s=23","alt":"maximum recorded lifespan | 69 years  (in captivity)weight | 2.3 lb  (pounds)maximum recorded wingspan | 4.1 feet","width":391,"title":"maximum recorded lifespan | 69 years  (in captivity)weight | 2.3 lb  (pounds)maximum recorded wingspan | 4.1 feet","height":100},"plaintext":"maximum recorded lifespan | 69 years  (in captivity)weight | 2.3 lb  (pounds)maximum recorded wingspan | 4.1 feet","title":"Basic properties"},{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26861f9485ga5452748d000019ed3b02h610c6bh?MSPStoreType=image/gif&s=23","alt":"maximum recorded weight | 2.8 lb  (pounds)  (human: 1400 lb)","width":423,"title":"maximum recorded weight | 2.8 lb  (pounds)  (human: 1400 lb)","height":36},"plaintext":"maximum recorded weight | 2.8 lb  (pounds)  (human: 1400 lb)","title":"Physical dimensions"},{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26871f9485ga5452748d000024f3cche73be746e?MSPStoreType=image/gif&s=23","alt":"cerebralization index (ratio of brain weight to spinal cord weight) | 19  (human: 170)heart weight | 0.5 oz  (ounces)  (human: 11 oz)","width":546,"title":"cerebralization index (ratio of brain weight to spinal cord weight) | 19  (human: 170)heart weight | 0.5 oz  (ounces)  (human: 11 oz)","height":85},"plaintext":"cerebralization index (ratio of brain weight to spinal cord weight) | 19  (human: 170)heart weight | 0.5 oz  (ounces)  (human: 11 oz)","title":"Internal organs"},{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26881f9485ga5452748d00002dd55i73e4hhd305?MSPStoreType=image/gif&s=23","alt":"(typical values, except where otherwise noted)","width":249,"title":"(typical values, except where otherwise noted)","height":18},"plaintext":"(typical values, except where otherwise noted)","title":""}],"numsubpods":4,"id":"SpeciesDataPhysicalProperties","position":500,"title":"Biological properties","error":false,"states":{"count":2,"state":[{"input":"SpeciesDataPhysicalProperties__Show metric","name":"Show metric"},{"input":"SpeciesDataPhysicalProperties__More","name":"More"}]}},{"scanner":"Data","subpod":{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26891f9485ga5452748d00002770a3h7a323a24d?MSPStoreType=image/gif&s=23","alt":"","width":150,"title":"","height":101},"imagesource":"http://www.eol.org/pages/1177364","plaintext":"","title":""},"numsubpods":1,"id":"Image:SpeciesData","position":600,"title":"Image","error":false},{"scanner":"Data","subpod":{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26901f9485ga5452748d000036642dd0802e73c9?MSPStoreType=image/gif&s=23","alt":"Linnaeus, 1758","width":100,"title":"Linnaeus, 1758","height":18},"plaintext":"Linnaeus, 1758","title":""},"numsubpods":1,"id":"SpeciesAuthorithy:SpeciesData","position":700,"title":"Species authority","error":false},{"scanner":"Species","subpod":{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26911f9485ga5452748d000015b88iaiibid28c9?MSPStoreType=image/gif&s=23","alt":"Corvus albicollis  |  Corvus albus  |  Corvus bennetti  | ...  | (total: 44)","width":447,"title":"Corvus albicollis  |  Corvus albus  |  Corvus bennetti  | ...  | (total: 44)","height":22},"plaintext":"Corvus albicollis  |  Corvus albus  |  Corvus bennetti  | ...  | (total: 44)","title":""},"numsubpods":1,"id":"SpeciesScannerOtherMembersOfSpecies","position":800,"title":"Other members of genus Corvus","error":false,"states":{"count":1,"state":{"input":"SpeciesScannerOtherMembersOfSpecies__More","name":"More"}}},{"scanner":"Species","subpod":{"img":{"src":"http://www5b.wolframalpha.com/Calculate/MSP/MSP26921f9485ga5452748d000056i1d379face7ei0?MSPStoreType=image/gif&s=23","alt":"Corvus corax subsp. clarionensis  |  Corvus corax subsp. corax  |  Corvus corax subsp. principalis  | ...  | (total: 4)","width":522,"title":"Corvus corax subsp. clarionensis  |  Corvus corax subsp. corax  |  Corvus corax subsp. principalis  | ...  | (total: 4)","height":43},"plaintext":"Corvus corax subsp. clarionensis  |  Corvus corax subsp. corax  |  Corvus corax subsp. principalis  | ...  | (total: 4)","title":""},"numsubpods":1,"id":"SpeciesScannerMembersOfSpecies","position":900,"title":"Members of species Corvus corax","error":false,"states":{"count":1,"state":{"input":"SpeciesScannerMembersOfSpecies__More","name":"More"}}}],"sources":{"count":1,"source":{"text":"Species data","url":"http://www.wolframalpha.com/sources/SpeciesDataSourceInformationNotes.html"}},"timing":4.544,"parsetimedout":false,"profile":"EnterDoQuery:0.,StartWrap:4.54371","error":false,"version":2.6,"datatypes":"Species","related":"http://www5b.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa26811f9485ga5452748d000049556951546128d6&s=23","success":true,"host":"http://www5b.wolframalpha.com","parsetiming":0.384,"id":"MSPa26801f9485ga5452748d00005188gd6e0f39c2h9","numpods":8,"timedoutpods":"","recalculate":"http://www5b.wolframalpha.com/api/v2/recalc.jsp?id=MSPa26791f9485ga5452748d00004gdcacbd2ge6efg1&s=23"}}

0 个答案:

没有答案