android平台上的json解析

来源:互联网 发布:农村淘宝考试题和答案 编辑:程序博客网 时间:2024/06/07 18:02
json数据格式解析分为两种:一种是普通的,一种是带有数组形式的。 

普通形式的:服务器端返回的json数据格式如下:

{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
带数组形式的:服务器端返回的数据格式为:

{"calendar":
    {"calendarlist": 
        [ 
           {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false}, 
           {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false} 
        ]
    }
}
所以解析上面两种格式的实例如下:

package pan.gch.demo;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    private String simpleJSON = "{'userbean':{'Uid':'100196','Showname':'\u75af\u72c2\u7684\u7334\u5b50','Avtar':null,'State':1}}";
    private String complexJSON = "{'calendar':{'calendarlist':[{'calendar_id':'1705','title':'(\u4eb2\u5b50)ddssd','category_name':'\u9ed8\u8ba4\u5206\u7c7b','showtime':'1288927800','endshowtime':'1288931400','allDay':false},{'calendar_id':'1706','title':'(\u65c5\u884c)','category_name':'\u9ed8\u8ba4\u5206\u7c7b','showtime':'1288933200','endshowtime':'1288936800','allDay':false}]}}";
    private TextView mSimpleJSONTextView;
    private TextView mComplexJSONTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mSimpleJSONTextView = (TextView) findViewById(R.id.textView1);
        mComplexJSONTextView = (TextView) findViewById(R.id.textView2);
        try {
            JSONObject simpleJsonObject = new JSONObject(simpleJSON)
                    .getJSONObject("userbean");
            String uidString = simpleJsonObject.getString("Uid");
            String shownameString = simpleJsonObject.getString("Showname");
            String avtarString = simpleJsonObject.getString("Avtar");
            int state = simpleJsonObject.getInt("State");
            mSimpleJSONTextView.setText(uidString + "\t" + shownameString
                    + "\t" + avtarString + "\t" + state);

            JSONObject complexObject = new JSONObject(complexJSON)
                    .getJSONObject("calendar");
            String complexString = "";
            for (int i = 0; i < complexObject.getJSONArray("calendarlist")
                    .length(); i++) {
                JSONObject complexJsonObject = (JSONObject) (complexObject
                        .getJSONArray("calendarlist").opt(i));
                String complexItemString = null;
                String calendar_id = complexJsonObject.getString("calendar_id");

                String titleString = complexJsonObject.getString("title");
                String categoryString = complexJsonObject
                        .getString("category_name");
                String showString = complexJsonObject.getString("showtime");
                String endShowString = complexJsonObject
                        .getString("endshowtime");
                Boolean allDay = complexJsonObject.getBoolean("allDay");
                complexItemString = calendar_id + "\t" + titleString + "\t"
                        + categoryString + "\t" + showString + "\t" + "\t"
                        + endShowString + "\t";
                if (allDay == true) {
                    complexItemString += "true";
                } else {
                    complexItemString += "false";
                }
                complexString += (complexItemString + "\n");

            }
            mComplexJSONTextView.setText(complexString);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            System.out.println("-------------error" + e);
            e.printStackTrace();
        }
    }
}


执行结果如下图:

0 0