1.Android中解析json程序代码

  1. Android程序解析json数据可以通过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数据

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\",\"数学\":\"78\",\"计算机\":\"99\"}";

                   String value1 = null;

                   try {

                            // 将字符串转换成jsonObject对象

                            JSONObject myJsonObject = new JSONObject(jsonMessage);

                            // 获取对应的值

                            value1 = myJsonObject.getString("数学");

                            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("地理");

                                     // 获取数组中对象的对象

                                     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;

         }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值