如何通过Json Array获取数据

时间:2013-09-20 06:38:09

标签: android json

{"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","id":{"$t":"http://gdata.youtube.com/feeds/base/videos"},"updated":{"$t":"2013-09-20T06:30:46.786Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://gdata.youtube.com/schemas/2007#video"}],"title":{"$t":"Videos","type":"text"},"logo":{"$t":"http://www.gstatic.com/youtube/img/logo.png"},"link":[{"rel":"alternate","type":"text/html","href":"http://www.youtube.com"},{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"http://gdata.youtube.com/feeds/base/videos"},{"rel":"http://schemas.google.com/g/2005#batch","type":"application/atom+xml","href":"http://gdata.youtube.com/feeds/base/videos/batch"},{"rel":"self","type":"application/atom+xml","href":"http://gdata.youtube.com/feeds/base/videos?alt=json&author=astrobixweb&start-index=1&max-results=10&orderby=published"},{"rel":"next","type":"application/atom+xml","href":"http://gdata.youtube.com/feeds/base/videos?alt=json&author=astrobixweb&start-index=11&max-results=10&orderby=published"}],"author":[{"name":{"$t":"YouTube"},"uri":{"$t":"http://www.youtube.com/"}}],"generator":{"$t":"YouTube data API","version":"2.1","uri":"http://gdata.youtube.com"},"openSearch$totalResults":{"$t":189},"openSearch$startIndex":{"$t":1},"openSearch$itemsPerPage":{"$t":10},"entry":[{"id":{"$t":"http://gdata.youtube.com/feeds/base/videos/cpqLJrFNBSY"},"published":{"$t":"2013-09-18T00:30:03.000Z"},"updated":{"$t":"2013-09-18T04:59:49.000Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://gdata.youtube.com/schemas/2007#video"}],"title":{"$t":"Analysis of Religious Inclination and Mystical Powers through Palmistry","type":"text"},"content":{"$t":"\u003cdiv style=\"color: #000000;font-family: Arial, Helvetica, sans-serif;     font-size:12px; font-size: 12px; width: 555px;\"\u003e\n\u003ctable cellspacing=\"0\" cellpadding=\"0\" border=\"0\"\u003e\u003ctbody\u003e\u003ctr\u003e\u003ctd width=\"140\" valign=\"top\" rowspan=\"2\"\u003e\u003cdiv style=\"border: 1px solid #999999; margin: 0px 10px 5px 0px;\"\u003e\u003ca href=\"http://www.youtube.com/watch?v=cpqLJrFNBSY&feature=youtube_gdata\"\u003e\u003cimg alt=\"\" src=\"http://i.ytimg.com/vi/cpqLJrFNBSY/0.jpg\"\u003e\u003c/a\u003e\u003c/div\u003e\u003c/td\u003e\n\u003ctd width=\"256\" valign=\"top\"\u003e\u003cdiv style=\"font-size: 12px; font-weight: bold;\"\u003e\u003ca style=\"font-size: 15px; font-weight: bold;                  font-decoration: none;\" href=\"http://www.youtube.com/watch?v=cpqLJrFNBSY&feature=youtube_gdata\"\u003eAnalysis of Religious Inclination and Mystical Powers through Palmistry\u003c/a\u003e\n\u003cbr\u003e\u003c/div\u003e\n\u003cdiv style=\"font-size: 12px; margin: 3px 0px;\"\u003e\u003cspan\u003eIn this webcast, we will talk about certain indications in your palm that provide information about your inclination towards religion and mystical powers. Be...\u003c/span\u003e\u003c/div\u003e\u003c/td\u003e\n\u003ctd style=\"font-size: 11px; line-height: 1.4em; padding-left: 20px;             padding-top: 1px;\" width=\"146\" valign=\"top\"\u003e\u003cdiv\u003e\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eFrom:\u003c/span\u003e\n\u003ca href=\"http://www.youtube.com/channel/UCbCzg6K98Enggf4uQjYCLdA\"\u003eastrobixweb\u003c/a\u003e\u003c/div\u003e\n\u003cdiv\u003e\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eViews:\u003c/span\u003e\n10\u003c/div\u003e\n\u003cdiv style=\"white-space: nowrap;text-align: left\"\u003e\u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e \u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e \u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e \u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e \u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e\u003c/div\u003e\n\u003cdiv style=\"font-size: 11px;\"\u003e1\n\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eratings\u003c/span\u003e\u003c/div\u003e\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eTime:\u003c/span\u003e\n\u003cspan style=\"color: #000000; font-size: 11px; font-weight: bold;\"\u003e04:16\u003c/span\u003e\u003c/td\u003e\n\u003ctd style=\"font-size: 11px; padding-left: 20px;\"\u003e\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eMore in\u003c/span\u003e\n\u003ca href=\"http://www.youtube.com/videos?c=27\"\u003eEducation\u003c/a\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\u003c/table\u003e\u003c/div\u003e","type":"html"},"link":[{"rel":"alternate","type":"text/html","href":"http://www.youtube.com/watch?v=cpqLJrFNBSY&feature=youtube_gdata"},{"rel":"self","type":"application/atom+xml","href":"http://gdata.youtube.com/feeds/base/videos/cpqLJrFNBSY"}],"author":[{"name":{"$t":"astrobixweb"},"uri":{"$t":"http://gdata.youtube.com/feeds/base/users/astrobixweb"}}]},{"id":{"$t":"http://gdata.youtube.com/feeds/base/videos/2wl6nlO5RVg"},"published":{"$t":"2013-09-16T00:30:05.000Z"},"updated":{"$t":"2013-09-19T06:26:38.000Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://gdata.youtube.com/schemas/2007#video"}],"title":{"$t":"Analysis of Your Health through Palmistry","type":"text"},"content":{"$t":"\u003cdiv style=\"color: #000000;font-family: Arial, Helvetica, sans-serif;     font-size:12px; font-size: 12px; width: 555px;\"\u003e\n\u003ctable cellspacing=\"0\" cellpadding=\"0\" border=\"0\"\u003e\u003ctbody\u003e\u003ctr\u003e\u003ctd width=\"140\" valign=\"top\" rowspan=\"2\"\u003e\u003cdiv style=\"border: 1px solid #999999; margin: 0px 10px 5px 0px;\"\u003e\u003ca href=\"http://www.youtube.com/watch?v=2wl6nlO5RVg&feature=youtube_gdata\"\u003e\u003cimg alt=\"\" src=\"http://i.ytimg.com/vi/2wl6nlO5RVg/0.jpg\"\u003e\u003c/a\u003e\u003c/div\u003e\u003c/td\u003e\n\u003ctd width=\"256\" valign=\"top\"\u003e\u003cdiv style=\"font-size: 12px; font-weight: bold;\"\u003e\u003ca style=\"font-size: 15px; font-weight: bold;                  font-decoration: none;\" href=\"http://www.youtube.com/watch?v=2wl6nlO5RVg&feature=youtube_gdata\"\u003eAnalysis of Your Health through Palmistry\u003c/a\u003e\n\u003cbr\u003e\u003c/div\u003e\n\u003cdiv style=\"font-size: 12px; margin: 3px 0px;\"\u003e\u003cspan\u003eUntil now, we have already discussed a lot of subjects related to Palmistry. In this webcast, we will talk about various aspects of Palmistry which can help ...\u003c/span\u003e\u003c/div\u003e\u003c/td\u003e\n\u003ctd style=\"font-size: 11px; line-height: 1.4em; padding-left: 20px;             padding-top: 1px;\" width=\"146\" valign=\"top\"\u003e\u003cdiv\u003e\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eFrom:\u003c/span\u003e\n\u003ca href=\"http://www.youtube.com/channel/UCbCzg6K98Enggf4uQjYCLdA\"\u003eastrobixweb\u003c/a\u003e\u003c/div\u003e\n\u003cdiv\u003e\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eViews:\u003c/span\u003e\n51\u003c/div\u003e\n\u003cdiv style=\"white-space: nowrap;text-align: left\"\u003e\u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e \u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e \u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e \u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e \u003cimg style=\"border: 0px none; margin: 0px; padding: 0px;                    vertical-align: middle; font-size: 11px;\" align=\"top\" alt=\"\" src=\"http://gdata.youtube.com/static/images/icn_star_full_11x11.gif\"\u003e\u003c/div\u003e\n\u003cdiv style=\"font-size: 11px;\"\u003e2\n\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eratings\u003c/span\u003e\u003c/div\u003e\u003c/td\u003e\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eTime:\u003c/span\u003e\n\u003cspan style=\"color: #000000; font-size: 11px; font-weight: bold;\"\u003e05:52\u003c/span\u003e\u003c/td\u003e\n\u003ctd style=\"font-size: 11px; padding-left: 20px;\"\u003e\u003cspan style=\"color: #666666; font-size: 11px;\"\u003eMore in\u003c/span\u003e\n\u003ca href=\"http://www.youtube.com/videos?c=27\"\u003eEducation\u003c/a\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\u003c/table\u003e\u003c/div\u003e","type":"html"},"link":[{"rel":"alternate","type":"text/html","href":"http://www.youtube.com/watch?v=2wl6nlO5RVg&feature=youtube_gdata"},{"rel":"self","type":"appl...

//这是我提取的课程

 public static void GroupResult(String url)
 {

    try{
      JSONArray jArray;
      JSONObject jObject;

     response=GetJsonObject.sendRequest(url);

     if(response == null){
            return;
        }

     jObject=new JSONObject(response);
     jArray=jObject.getJSONArray("Guru");
     mList.clear();
     for(int i=0;i<jArray.length();i++){

         mData=new AstrobixData();
         jObject=jArray.getJSONObject(i);
         mData.SetSrc(jObject.getString("src"));
         //String temp=jObject.getString("src");
         mList.add(mData);


     }



    }catch(Exception e){

     }

}

//这是我的JsonObject类

public class GetJsonObject {

public GetJsonObject(){

}


//String Method to fetech data from server
public static String sendRequest(String url) {
    String result = "";
    try {

        HttpClient client = new DefaultHttpClient();
        HttpParams httpParameters = client.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
        HttpConnectionParams.setSoTimeout(httpParameters, 5000);
        HttpConnectionParams.setTcpNoDelay(httpParameters, true);
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        HttpResponse response = client.execute(request);
        InputStream ips = response.getEntity().getContent();

        BufferedReader buf = new BufferedReader(new InputStreamReader(ips,
                "UTF-8"));

        StringBuilder sb = new StringBuilder();
        String s;
        while (true) {
            s = buf.readLine();
            if (s == null || s.length() == 0)
                break;
            sb.append(s);

        }
        buf.close();
        ips.close();
        result = sb.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}
}

有谁能告诉我如何获取这个彻底的Json数组或Json对象的数据? 在这里我也把网址,我已经把我尝试过的一些 所有数据都变成一个变量.... 这里是src名称对象我想要搜索的内容如下:http://i.ytimg.com/vi/cpqLJrFNBSY/0.jpg

4 个答案:

答案 0 :(得分:1)

首先,您的JSON响应无效,第二个

您的代码行

 jArray=jObject.getJSONArray("Guru");

您的回复中没有名为“Guru”的元素。

理想的方式应该是,

String jsonItem = JSONObject.getJSONArray("Array Name").getString("string name");

您可以使用此link来验证json的有效性并查看树结构。

答案 1 :(得分:0)

以下是您的解决方案,

String href = JSONResponse.getJSONObject("feed").getJSONArray("entry").getJSONObject("0").getJSONArray("link").getJSONObject("0").getString("href");

您可以通过树以相同的方式解析其他值。 希望这会有所帮助。

答案 2 :(得分:0)

这是你的j-son链接获得标题的解决方案。就像这样,你可以得到href和你想要的一切。

  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

ArrayList<String> title_list = new ArrayList<String>();

        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/base/videos?max-results=10&start-index=1&alt=json&orderby=published&author=astrobixweb");

        try
        {
        HttpResponse response = httpclient.execute(request);
        HttpEntity resEntity = response.getEntity();
        String _response=EntityUtils.toString(resEntity); // content will be consume only once

        //Log.v("Test", ""+_response);

        jsonarray = new JSONArray("["+_response+"]");
        json = jsonarray.getJSONObject(0);
        String feed = (json.getString("feed"));

        Log.v("feed", ""+feed);

        try{


            jsonarray = new JSONArray("["+feed+"]");

            json = jsonarray.getJSONObject(0);

            String entry  = json.getString("entry");

            jsonarray = new JSONArray(entry);

            for (int i = 0; i < jsonarray.length(); i++) {

                json = jsonarray.getJSONObject(i);

                 String title_array  = json.getString("title");

                 jsonArray1 = new JSONArray("["+title_array+"]");

                 String title = jsonArray1.getJSONObject(0).getString("$t");

                 title_list.add(title);

        }
            Log.v("title", ""+title_list);

        }catch (Exception e){
            e.printStackTrace();
        }
        }catch (Exception e){
            e.printStackTrace();
        }
}

答案 3 :(得分:0)

检查出来:

String str = "<assign your json string>"
        try {
            JSONObject jObject=new JSONObject(str);
            jObject = jObject.getJSONObject("feed");
            JSONObject jObject1 = jObject.getJSONObject("title");
            Log.d("$t==", jObject1.getString("$t"));
            Log.d("type==", jObject1.getString("type"));
            JSONArray jArrayObject = new JSONArray(jObject.getString("link"));
            for (int i = 0; i<jArrayObject.length(); i++) {
                Log.d("href==", jArrayObject.getJSONObject(i).getString("href").toString());
            }
            JSONArray jArrayObject5 = new JSONArray(jObject.getString("entry"));
            for (int i = 0; i<jArrayObject5.length(); i++) {
                JSONObject jObject3 = jArrayObject5.getJSONObject(i).getJSONObject("title");
                Log.d("$t==", jObject3.getString("$t"));
                Log.d("type==", jObject3.getString("type"));
                JSONArray jArrayObject6 =  new JSONArray(jArrayObject5.getJSONObject(i).getString("link").toString());
                for (int j = 0; j<jArrayObject6.length(); j++) {
                    Log.d("href==", jArrayObject6.getJSONObject(j).getString("href").toString());
                }

            }
        } catch (Exception e) {
        }