1.Android中解析json程序代码
Android程序解析json數(shù)據(jù)可以通過gson的方式,這種情況需要導入相應的jar包。測試代碼如下:
| @Override ?? protected void onCreate(Bundle savedInstanceState) { ????? super.onCreate(savedInstanceState); ????? setContentView(R.layout.activity_main); ? ????? if (savedInstanceState == null) { ???????? getSupportFragmentManager().beginTransaction() ??????????????? .add(R.id.container, new PlaceholderFragment()).commit(); ????? } ? ????? //第一種方式: ????? String jsonString = "{\"id\":\"1378230362\",\"name\":\"360U1378230362\"}"; ????? System.out.println(jsonString); ????? jsonString = "[" + jsonString + "]"; ????? try { ???????? JsonReader reader = new JsonReader(new StringReader(jsonString)); ???????? reader.beginArray(); ???????? while (reader.hasNext()) { ??????????? reader.beginObject(); ??????????? while (reader.hasNext()) { ??????????????? String tagName = reader.nextName(); ??????????????? if (tagName.equals("id")) { ?????????????????? Toast.makeText(this, "id:" + reader.nextString(), 1000).show(); ?????????????????? System.out.println(reader.nextString()); ??????????????? } else if (tagName.equals("name")) { ?? ??????????????? Toast.makeText(this, "name:" + reader.nextString(), 1000).show(); ??????????????? ?? //System.out.println(reader.nextString()); ??????????????? } ??????????? } ??????????? reader.endObject(); ???????? } ???????? reader.endArray(); ????? } catch (Exception e) { ???????? e.printStackTrace(); ????? } ?? } |
2 通過Android中的JSONObject的方式解析JSON數(shù)據(jù)
| package com.example.jsontest2; ? import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; ? import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.Toast; ? public class MainActivity extends Activity { ? ???????? @Override ???????? protected void onCreate(Bundle savedInstanceState) { ?????????????????? super.onCreate(savedInstanceState); ?????????????????? setContentView(R.layout.activity_main); ?????????????????? ?????????????????? String jsonMessage = "{\"語文\":\"88\",\"數(shù)學\":\"78\",\"計算機\":\"99\"}"; ?????????????????? String value1 = null; ?????????????????? try { ??????????????????????????? // 將字符串轉(zhuǎn)換成jsonObject對象 ??????????????????????????? JSONObject myJsonObject = new JSONObject(jsonMessage); ??????????????????????????? // 獲取對應的值 ??????????????????????????? value1 = myJsonObject.getString("數(shù)學"); ??????????????????????????? Toast.makeText(this, "value1:" + value1, 3000).show(); ?????????????????? } catch (JSONException e) {} ? ?????????????????? System.out.println("value1=" + value1); ? ?????????????????? // JSONArray ?????????????????? jsonMessage = "[{'num':'成績', '外語':88, '歷史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," ???????????????????????????????????? + "{'num':'興趣', '外語':28, '歷史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," ???????????????????????????????????? + "{'num':'愛好', '外語':48, '歷史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]"; ?????????????????? JSONArray myJsonArray; ?????????????????? try { ??????????????????????????? myJsonArray = new JSONArray(jsonMessage); ? ??????????????????????????? for (int i = 0; i < myJsonArray.length(); i++) { ???????? ??????????????????????????? // 獲取每一個JsonObject對象 ???????????????????????????????????? JSONObject myjObject = myJsonArray.getJSONObject(i); ? ???????????????????????????????????? // 獲取每一個對象中的值 ???????????????????????????????????? String numString = myjObject.getString("num"); ???????????????????????????????????? int englishScore = myjObject.getInt("外語"); ???????????????????????????????????? int historyScore = myjObject.getInt("歷史"); ???????????????????????????????????? int geographyScore = myjObject.getInt("地理"); ???????????????????????????????????? // 獲取數(shù)組中對象的對象 ???????????????????????????????????? JSONObject myjObject2 = myjObject.getJSONObject("object"); ???????????????????????????????????? String aaaString = myjObject2.getString("aaa"); ???????????????????????????????????? System.out.println("aaaString=" + aaaString); ? ???????????????????????????????????? System.out.println("numString=" + numString); ???????????????????????????????????? System.out.println("englishScore=" + englishScore); ???????????????????????????????????? System.out.println("historyScore=" + historyScore); ???????????????????????????????????? System.out.println("geographyScore=" + geographyScore); ??????????????????????????? } ?????????????????? } catch (JSONException e) { ?????????????????? } ???????? } ? ???????? @Override ???????? public boolean onCreateOptionsMenu(Menu menu) { ?????????????????? // Inflate the menu; this adds items to the action bar if it is present. ?????????????????? getMenuInflater().inflate(R.menu.main, menu); ?????????????????? return true; ???????? } } |
總結(jié)
以上是生活随笔為你收集整理的1.Android中解析json程序代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贵绳股份为何是军工
- 下一篇: 关于Eclipse创建Android项目