Android實現(xiàn)讀寫JSON數(shù)據(jù)的方法
本文實例講述了Android實現(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); }
希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
詳解Android 多級聯(lián)動控件實現(xiàn)思路討論
這篇文章主要介紹了詳解Android 多級聯(lián)動控件實現(xiàn)思路討論,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Kotlin自定義View系列教程之標(biāo)尺控件(選擇身高、體重等)的實現(xiàn)
這篇文章主要給大家介紹了關(guān)于Kotlin自定義View系列教程之標(biāo)尺控件(選擇身高、體重等)實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07解決Android Studio Design界面不顯示layout控件的問題
這篇文章主要介紹了解決Android Studio Design界面不顯示layout控件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Activity實例詳解之啟動activity并返回結(jié)果
這篇文章主要介紹了Activity實例詳解之啟動activity并返回結(jié)果 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Android 開發(fā)之旅:詳解view的幾種布局方式及實踐
這篇文章主要介紹了Android 開發(fā)之旅:詳解view的幾種布局方式及實踐,具有一定的參考價值,有需要的可以了解一下。2016-12-12android實現(xiàn)圖片上傳功能(springMvc)
這篇文章主要為大家詳細(xì)介紹了android結(jié)合springMvc實現(xiàn)圖片上傳的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03