2012-11-02 35 views
-8

可能重複:
How do I parse JSON from a Java HTTPResponse?
How to parse json string in Android?如何在Android中獲取和解析JSON對象?

如何可以解析此JSON字符串

{ 
"apiVersion": "2.1", 
"data": { 
    "id": "pHuoDqcIyqk", 
    "uploaded": "2012-10-29T16:08:15.000Z", 
    "updated": "2012-11-02T08:48:28.000Z", 
    "uploader": "googlenexus", 
    "category": "Tech", 
    "title": "Nexus: Ask Me Anything", 
    "description": "The Best of Google, now in 3 sizes. Introducing Nexus 4, Nexus 7 and Nexus 10. The new smartphone and tablets from Google. Shop now at play.google.com/nexus", 
    "thumbnail": { 
     "sqDefault": "http://i.ytimg.com/vi/pHuoDqcIyqk/default.jpg", 
     "hqDefault": "http://i.ytimg.com/vi/pHuoDqcIyqk/hqdefault.jpg" 
    }, 
    "player": { 
     "default": "http://www.youtube.com/watch?v=pHuoDqcIyqk&feature=youtube_gdata_player", 
     "mobile": "http://m.youtube.com/details?v=pHuoDqcIyqk" 
    }, 
    "content": { 
     "5": "http://www.youtube.com/v/pHuoDqcIyqk?version=3&f=videos&app=youtube_gdata", 
     "1": "rtsp://v8.cache5.c.youtube.com/CiILENy73wIaGQmpyginDqh7pBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp", 
     "6": "rtsp://v7.cache4.c.youtube.com/CiILENy73wIaGQmpyginDqh7pBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" 
    }, 
    "duration": 61, 
    "aspectRatio": "widescreen", 
    "rating": 4.8985643, 
    "likeCount": "5227", 
    "ratingCount": 5363, 
    "viewCount": 1038854, 
    "favoriteCount": 0, 
    "commentCount": 1442, 
    "accessControl": { 
     "comment": "allowed", 
     "commentVote": "allowed", 
     "videoRespond": "moderated", 
     "rate": "allowed", 
     "embed": "allowed", 
     "list": "allowed", 
     "autoPlay": "allowed", 
     "syndicate": "allowed" 
    } 
} 
} 

誰能告訴我如何撥打電話,以上述URL &得到一個JSON對象,然後解析它得到想要的信息作爲字符串?

+0

我沒有任何想法制作網絡電話,所以等待解決方案。請幫助 – vaibvorld

+0

有很多鏈接JSON解析........嘗試自己第一...鏈接之一是http:// www.androidhive.info/2012/01/android-json-parsing-tutorial/ –

+0

瞭解如何從本網站解析json字符串http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ –

回答

0

好太寬你問的問題..

1)請在URL中的http get請求。 (或使用this方法)並獲取響應字符串,即JSON。一些示例here

2)請參閱this答案以瞭解如何解析JSON。

0

簡單簡單....

String str = "" // your JSON string 
JSONObject json = new JSONObject(str); 
String apiVersion = json.getString("apiVersion"); 
// continue as before for the rest of it... 
0

你可以試試下面的方法。您可以傳遞URL並獲得響應結果字符串。 public static String result;

public static boolean connect(String url) { 
     boolean flag = false; 
     HttpClient httpclient = new DefaultHttpClient(); 

     // Prepare a request object 
     HttpGet httppost = new HttpGet(url); 
     Log.e("url", url); 

     // Execute the request 
     HttpResponse response; 
     try { 

      // https://api.vkontakte.ru/method/audio.search?uid=163398985&q=akoncount=100&access_token=2a4db0e223f0f5ab23f0f5ab5f23da5680223f023f1f5a3c696b018be9b17b9 

      response = httpclient.execute(httppost); 
      // Examine the response status 
      Log.i("vkontake", response.getStatusLine().toString() + "\n" 
        + response); 

      // Get hold of the response entity 
      HttpEntity entity = response.getEntity(); 
      // If the response does not enclose an entity, there is no need 
      // to worry about connection release 

      if (entity != null) { 

       // A Simple JSON Response Read 
       InputStream instream = entity.getContent(); 
       Log.d("Jsomn Activity", "---- is --- " + instream); 
       result = convertStreamToString(instream); 
       Log.d("Jsomn Activity", "---- Result --- " + result); 

       // now you have the string representation of the HTML request 
       instream.close(); 

      } else { 
       Log.d("Jsomn Activity", "---- is --- null "); 
      } 
      flag = true; 
      net = false; 
     } catch (Exception e) { 
      Log.d("Jsomn Activity", "---- Catch --- " + e.toString()); 
      flag = false; 
      e.printStackTrace(); 
     } finally { 
      return flag; 
     } 

    } 
0

有幾種方法可以做到這一點。一個簡單的方法是定義匹配您的數據的對象的結構和使用http://code.google.com/p/google-gson/往裏面:

final URL url = new URL("http://yourURL.com"); 
final InputStream openStream = url.openStream(); 
final InputStreamReader inputStreamReader = new InputStreamReader(openStream); 
final SearchResult searchResult = new Gson().fromJson(inputStreamReader, SearchResult.class); 

如果你的信息搜索結果類containts場如apiVersion,它將被填充來自JSON的數據。然後你有一個數據字段與其他字段等。這是一個非常簡單的方式來填充你的數據,但你將不得不鏡像對象模型中的JSON流的結構。