我的json数据看起来像 -
{
android: [
{
name: "link1",
link: "href="/downloads/-------------""
},
{
name: "link2",
link: "http://www.----------"
}
]
}
我必须显示仅从我的活动的json数据中获取的名称,以便在显示名称时它应该是可点击的,并且在点击该名称时它应该将其重定向到相应的链接。
**例如:**
当从json数据中提取NAME(link1)并单击时,它应该将其重定向到相应的链接"href="/downloads/-----------"
。
我很难让名字可以点击各自的链接。
答案 0 :(得分:1)
> coords <-which(mydata!=0, arr.ind=T)
> str(coords)
int [1:889222, 1:3] 40 40 41 40 41 40 41 40 41 40 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "dim1" "dim2" "dim3"
答案 1 :(得分: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&#34; namelink&#34;,包含带链接的名称...根据名称获取hashmap值(链接)
例如;
String linkretreived=namelinke.get(<key>);
答案 2 :(得分: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发出请求 private static String url =“http://www.yourjsonurl.com/json.php”;
// JSON节点名称 private static final String TAG_CONTACTS =“contacts”; private static final String TAG_ID =“id”; private static final String 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();
}