2015-05-18 76 views
0

我的JSON数据的模样 -读取并解析在Android的JSON数据并显示它

{ 
    android: [ 
       { 
         name: "link1", 
         link: "href="/downloads/-------------"" 

       }, 
       { 

        name: "link2", 
        link: "http://www.----------" 
       } 
      ] 
    } 

我要dispaly从JSON数据在我的活动获取ONLY地名,在这样一种方式,当名称显示它应该是可点击的,点击该名称后,它应该将其重定向到相应的链接。

**例如:** 当从json数据中提取NAME(link1)并单击它时,它应该将其重定向到相应的链接"href="/downloads/-----------"

我很难将名称点击到各自的链接。

+0

通过通过基本训练和一点在谷歌研究会,你应该能够找出如何做到这一点?如果有的话,这个问题其实是很多的副本中,还有很多其他问题。 – 2Dee

+0

可能重复的[如何在Android中解析JSON](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) –

+0

我强烈推荐[AndroidQuery](https:// code.google.com/p/android-query/)库用于此目的以及许多其他目的 – ygesher

回答

1
JsonArray root=response.getJsonArray("Android"); 

for(int i=0;i<root.size();i++) 
    { 
    JsonObject innerElement=root.getJsonObject(i); 

    String name=innerElement.getString("name"); 

    String name=innerElement.getString("link"); 
    } 
+0

您刚刚从json获取数据。我应该如何显示名称,以便在它被点击时将它重定向到相应的链接。 – Lucky

+0

使用散列表并将名称保存为key和url作为值,然后在散列表中搜索名称,您将获得url – JesusS

0
HashMap<String, String> namelink = new HashMap<String, String>(); 

JsonArray root=response.getJsonArray("android"); 
for(int i=0;i<root.size();i++) 
{ 
    JsonObject innerElement=root.getJsonObject(i); 
    String name=innerElement.getString("name"); 
    String link=innerElement.getString("link"); 
    namelink .put(name, link); 
} 

现在..the HashMap的 “namelink”,包含名称与链接...得到的HashMap值(链接)根据名称

为如;

String linkretreived=namelinke.get(<key>); 
0

参照此:How to parse JSON in Android你需要首先JSONParser类:

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() {} 

    public JSONObject getJSONFromUrl(String url) { 

     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
} 

然后宣告您在活动所需要的所有变量:

// URL,使请求 私有静态String url =“http://www.yourjsonurl.com/json.php”;

// JSON节点名称 private static final String TAG_CONTACTS =“contacts”; private static final String TAG_ID =“id”;私人静态最终字符串TAG_NAME =“name”;

//联系人JSONArray JSONArray contacts = null;

然后:

// Creating JSON Parser instance 
JSONParser jParser = new JSONParser(); 

// getting JSON string from URL 
JSONObject json = jParser.getJSONFromUrl(url); 

try { 
    // Getting Array of Contacts 
    contacts = json.getJSONArray(TAG_CONTACTS); 

    // looping through All Contacts 
    for(int i = 0; i < contacts.length(); i++){ 
     JSONObject c = contacts.getJSONObject(i); 

     // Storing each json item in variable 
     String id = c.getString(TAG_ID); 
     String name = c.getString(TAG_NAME); 
     String email = c.getString(TAG_EMAIL); 


    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
相关问题