如何解析json archive dot org

时间:2014-03-04 01:40:28

标签: java android json archive

我在尝试解析android应用程序上的archive.org json响应时遇到问题,来自此网址“http://archive.org/details/GoogleTVShow&output=json”,但我似乎无法弄清楚如何执行此操作,任何人都可以帮助我:

{
"server": "ia700607.us.archive.org",
"dir": "/21/items/GoogleTVShow",
"metadata": {
"identifier": [
  "GoogleTVShow"
],
"mediatype": [
  "audio"
],
"collection": [
  "opensource_audio"
],
"creator": [
  "Adrian Diaz R"
],
"date": [
  "2014-01-14"
],
"description": [
  "text\/>"
],
"language": [
  "eng"
],
"licenseurl": [
  "http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/"
],
"scanner": [
  "Internet Archive HTML5 Uploader 1.4.2"
],
"subject": [
  "google",
  "snowden",
  "adobe",
  "diaspora",
  "ebay",
  "aaron",
  "swartz",
  "foxconn",
  "anonymous",
  "wall",
  "street",
  "krokodil",
  "kim",
  "jong"
  ],
  "title": [
     "Google TV Show"
  ],
 "publicdate": [
  "2014-01-15 00:27:57"
  ],
 "addeddate": [
  "2014-01-15 00:27:57"
  ]
 },
"files": {
"\/01. Google TV Show.mp3": {
  "source": "original",
  "format": "VBR MP3",
  "length": "62.44",
  "height": "0",
  "width": "0",
  "mtime": "1389745677",
  "size": "1498466",
  "md5": "71213f91d5d88bae84951bb270b9eefb",
  "crc32": "b054c268",
  "sha1": "0a75b7d06f70d5c1c26737892c1544f7539bd955",
  "external-identifier": "urn:acoustid:unknown"
 },
 "\/01. Google TV Show.ogg": {
  "source": "derivative",
  "format": "Ogg Vorbis",
  "original": "01. Google TV Show.mp3",
  "mtime": "1389746385",
  "size": "638625",
  "md5": "d6ab6fda6e342735567a505e3652b0d1",
  "crc32": "669f4f0c",
  "sha1": "1e6ebb29746a1ce8f7041d0de7c3eb6e17ef0d0b",
  "length": "62.43",
  "height": "0",
  "width": "0"
 },
 "\/02. Ebay ADN wholesale.mp3": {
  "source": "original",
  "format": "VBR MP3",
  "length": "85.95",
  "height": "0",
  "width": "0",
  "mtime": "1389745768",
  "size": "2062697",
  "md5": "6b85ed59169f377a0808e8b0fac93d61",
  "crc32": "8d3ce6dd",
  "sha1": "fd145b28739dc86ee73969c1b3d7c9f4ba0bac7b",
  "external-identifier": "urn:acoustid:unknown"
 },
 "\/02. Ebay ADN wholesale.ogg": {
  "source": "derivative",
  "format": "Ogg Vorbis",
  "original": "02. Ebay ADN wholesale.mp3",
  "mtime": "1389746374",
  "size": "943307",
  "md5": "5f7f84263fa5ac9935e89e2265bec021",
  "crc32": "7f9845d3",
  "sha1": "214d8f590c6391dc4e969031aaee464385416ee4",
  "length": "85.94",
  "height": "0",
  "width": "0"..........................

1 个答案:

答案 0 :(得分:0)

希望我的下面的代码可以帮助你解析你的json:

/** An async task to prepare target image mapping list. */
    private class PrepareMapTask extends AsyncTask<String, Integer, Boolean>
    {
        // Initialize with invalid value
        private int mPrepareResult = -1;
        private String mJsonString = null;

        protected Boolean doInBackground(String... urls)
        {
            mJsonString = downloadFileFromInternet(urls[0]);
            if(mJsonString == null /*|| mJsonString.isEmpty()*/)
                return false;

            JSONObject jObject = null;
            try {
                jObject = new JSONObject(mJsonString);
                JSONArray jsonImageArray = jObject.getJSONArray("imageTarget");
                JSONArray jsonUrlArray = jObject.getJSONArray("videoUrls");
                JSONArray jsonVideoOrUrlArray = jObject.getJSONArray("videoOrUrl");
                if (jsonImageArray == null || jsonUrlArray == null)
                    return false;
                for (int i = 0; i<jsonImageArray.length(); i++){ 
                    mapTargetUrl.put(jsonImageArray.get(i).toString(), jsonUrlArray.get(i).toString());
                    mVideoOrUrl.add(jsonVideoOrUrlArray.get(i).toString());
                }
            } catch (JSONException e) {
                e.printStackTrace();
                return false;
            }

            return true;
        }

        protected void onPostExecute(Boolean result)
        {
        }

        private String downloadFileFromInternet(String url)
        {
            if(url == null /*|| url.isEmpty() == true*/)
                new IllegalArgumentException("url is empty/null");
            StringBuilder sb = new StringBuilder();
            InputStream inStream = null;
            try
            {
                url = urlEncode(url);
                URL link = new URL(url);
                inStream = link.openStream();
                int i;
                int total = 0;
                byte[] buffer = new byte[8 * 1024];
                while((i=inStream.read(buffer)) != -1)
                {
                    if(total >= (1024 * 1024))
                    {
                        return "";
                    }
                    total += i;
                    sb.append(new String(buffer,0,i));
                }
            }catch(Exception e )
            {
                e.printStackTrace();
                return null;
            }catch(OutOfMemoryError e)
            {
                e.printStackTrace();
                return null;
            }
            return sb.toString();
        }

        private String urlEncode(String url)
        {
            if(url == null /*|| url.isEmpty() == true*/)
                return null;
            url = url.replace("[","");
            url = url.replace("]","");
            url = url.replaceAll(" ","%20");
            return url;
        }

    }

我的Json:

{
        "imageTarget": [
        "image1",
        "image2",

    ],
        "videoUrls": [
        "http://www.youtube.com/watch?v=QoZRHLmUKtM",
        "http://ar.qualcomm.at"
    ],
        "videoOrUrl": [
        "video",
        "url"

    ]
}