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.

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. Commented May 18, 2015 at 13:14
  • possible duplicate of How to parse JSON in Android Commented May 18, 2015 at 13:20
  • I highly recommend the AndroidQuery library for this and many other purposes Commented May 18, 2015 at 16:00

3 Answers 3

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");
  }
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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
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>);

Comments

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.