0

My json data looks like -

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

              },
              {

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

I have to dispaly ONLY NAMES fetched from json data on my activity ,in such a way that when names are displayed it should be clickable and on clicking that name it should redirect it to the respective links.

**For Example : ** When NAME(link1) is fetched from json data and is clicked, it should redirect it to the respective link "href="/downloads/-----------" .

I am facing difficulty in making names clickable to their respective links.

Bruce_Wayne
  • 1,564
  • 3
  • 18
  • 41
Lucky
  • 61
  • 1
  • 3
  • By going through basic training and a bit of research on Google, you should be capable of finding how to do this... If anything, this question is actually a duplicate of many, many other questions. – 2Dee May 18 '15 at 13:14
  • possible duplicate of [How to parse JSON in Android](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android) – Ed Holloway-George May 18 '15 at 13:20
  • I highly recommend the [AndroidQuery](https://code.google.com/p/android-query/) library for this and many other purposes – ygesher May 18 '15 at 16:00

3 Answers3

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");
  }
Deepanshu Gandhi
  • 490
  • 3
  • 10
  • you have just fetched data from json. how i should display name so that when it would be clicked it would redirect it to the respective link. – Lucky May 18 '15 at 13:08
  • Use a hashmap and save name as key and url as value, then search for the name in the hashmap and you will get the url – JesusS May 18 '15 at 15:19
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);
}

now ..the hashmap "namelink",contain name with link...get hashmap value (link) according to name

for eg;

String linkretreived=namelinke.get(<key>);
Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23
0

Referring to this: How to parse JSON in Android you need a JSONParser class for first:

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;

    }
}

Then declare all variables you need in your activity:

// url to make request private static String url = "http://www.yourjsonurl.com/json.php";

// JSON Node names private static final String TAG_CONTACTS = "contacts"; private static final String TAG_ID = "id"; private static final String TAG_NAME = "name";

// contacts JSONArray JSONArray contacts = null;

and then:

// 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();
}
Community
  • 1
  • 1
Atlas91
  • 5,754
  • 17
  • 69
  • 141