1. 程式人生 > >Android JSON寫入,解析

Android JSON寫入,解析

package com.example.he.toby.jsondemo;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import 
org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; //
TODO: 2016/2/23 jsonobject 輕量級的資料交換格式 ,json定義的基本單元,主要包含的就是一對(key/values)的值,使用{} 括起來的一組資料 ,{key ,values} ,{key,[數值1,數值2,數值3]} // TODO: 2016/2/23 jsonarray 代表一種有序的數值,可以將物件的資訊變化為字串,所有的資料使用"[]"包裹,數值之間以","分隔,[數值1,數值2,數值3] public class MainActivity extends AppCompatActivity { private TextView textView
; private String data[] = {"www.baidu.com", "www.gigaset.com", "www.bbs.com"}; private String nameData[] = new String[]{"xxx", "yyy", "zzzz", "mmmm"}; private String companyTel = "1111111"; private Button button; String str = "[{\"id\":1,\"name\":hjw\"}]"; StringBuffer buffer = new StringBuffer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.textView = (TextView) findViewById(R.id.textview); this.button = (Button) findViewById(R.id.button); this.button.setOnClickListener(listener); JSONObject allData = new JSONObject(); // TODO: 2016/2/23 先建立最外邊的alldata物件 JSONArray sing = new JSONArray(); // TODO: 2016/2/23 建立新的jsonarray物件 for (int x = 0; x < data.length; x++) // TODO: 2016/2/23 通過陣列方式新增 { JSONObject temp = new JSONObject(); try { temp.put("myurl", data[x]); // TODO: 2016/2/23 設定要儲存的資料 temp.put("second", nameData[x]); } catch (JSONException e) { e.printStackTrace(); } sing.put(temp); Log.e("main", "儲存一個資訊 "); } try { allData.put("urldata", sing); allData.put("telephone", companyTel); // TODO: 2016/2/23 單獨新增 Log.e("main", "儲存所有資訊 "); } catch (JSONException e) { e.printStackTrace(); } if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(getApplicationContext(), "有外接儲存", Toast.LENGTH_SHORT).show(); return; // TODO: 2016/2/23 返回呼叫處 } File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "mldn" + File.separator + "json.txt"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } PrintStream out = null; // TODO: 2016/2/23 列印流 try { out = new PrintStream(new FileOutputStream(file)); //例項化列印流物件 out.print(allData.toString()); //輸出資料 Log.e("main", "write msg in json.txt"); // str = allData.toString(); textView.setText(allData.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); // TODO: 2016/2/23 關閉列印流 } } } // TODO: 2016/2/24 按鍵的監聽 View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: Log.e("button", "解析得到的資料"); try { List<Map<String, Object>> all = parseJson(str); Iterator<Map<String, Object>> iterator = all.iterator(); while (iterator.hasNext()) { Map<String, Object> map = iterator.next(); buffer.append("ID:" + map.get("id") + ",name: " + map.get("name")); Log.e("json", "結果: " + buffer); textView.setText(buffer); } } catch (Exception e) { e.printStackTrace(); } break; } } }; public List<Map<String, Object>> parseJson(String data) throws Exception { List<Map<String, Object>> all = new ArrayList<Map<String, Object>>(); JSONArray jsonArray = new JSONArray(data); for (int x = 0; x < jsonArray.length(); x++) { Map<String, Object> map = new HashMap<String, Object>(); JSONObject jsonObject = jsonArray.getJSONObject(x); map.put("id", jsonObject.getString("id")); map.put("name", jsonObject.getString("name")); all.add(map); } return all; } }