Android實(shí)現(xiàn)讀寫JSON數(shù)據(jù)的方法
本文實(shí)例講述了Android實(shí)現(xiàn)讀寫JSON數(shù)據(jù)的方法。分享給大家供大家參考。具體如下:
1. 解析JSON:
package de.vogella.android.twitter.json; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class ParseJSON extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String readTwitterFeed = readTwitterFeed(); try { JSONArray jsonArray = new JSONArray(readTwitterFeed); Log.i(ParseJSON.class.getName(), "Number of entries " + jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); Log.i(ParseJSON.class.getName(), jsonObject.getString("text")); } } catch (Exception e) { e.printStackTrace(); } } public String readTwitterFeed() { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet( "http://twitter.com/statuses/user_timeline/vogella.json"); try { HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader( new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } else { Log.e(ParseJSON.class.toString(), "Failed to download file"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return builder.toString(); } }
2. 生成JSON:
public void writeJSON() { JSONObject object = new JSONObject(); try { object.put("name", "Jack Hack"); object.put("score", new Integer(200)); object.put("current", new Double(152.32)); object.put("nickname", "Hacker"); } catch (JSONException e) { e.printStackTrace(); } System.out.println(object); }
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解Android 多級(jí)聯(lián)動(dòng)控件實(shí)現(xiàn)思路討論
這篇文章主要介紹了詳解Android 多級(jí)聯(lián)動(dòng)控件實(shí)現(xiàn)思路討論,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Kotlin自定義View系列教程之標(biāo)尺控件(選擇身高、體重等)的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Kotlin自定義View系列教程之標(biāo)尺控件(選擇身高、體重等)實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07解決Android Studio Design界面不顯示layout控件的問(wèn)題
這篇文章主要介紹了解決Android Studio Design界面不顯示layout控件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03android實(shí)現(xiàn)滑動(dòng)解鎖
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)滑動(dòng)解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04Android圖片上傳實(shí)現(xiàn)預(yù)覽效果
這篇文章主要介紹了Android圖片上傳實(shí)現(xiàn)預(yù)覽效果的相關(guān)資料,需要的朋友可以參考下2016-01-01Activity實(shí)例詳解之啟動(dòng)activity并返回結(jié)果
這篇文章主要介紹了Activity實(shí)例詳解之啟動(dòng)activity并返回結(jié)果 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android 開(kāi)發(fā)之旅:詳解view的幾種布局方式及實(shí)踐
這篇文章主要介紹了Android 開(kāi)發(fā)之旅:詳解view的幾種布局方式及實(shí)踐,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12android實(shí)現(xiàn)圖片上傳功能(springMvc)
這篇文章主要為大家詳細(xì)介紹了android結(jié)合springMvc實(shí)現(xiàn)圖片上傳的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03