关闭

json解析之四种基本格式的解析

386人阅读 评论(0) 收藏 举报
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String str = "";
        JSONObject jsonObject = null;
        String json = "";


        //直接一个对象的解析
        json = "{\"username\":\"zms\",\"age\":43,\"addr\":\"江西省高安市村前镇\"}";
        try {
            jsonObject = new JSONObject(json);
            str = "名字:" + jsonObject.getString("username") + " 年龄:" + jsonObject.getString("age") + " 地点:" + jsonObject.getString("addr") + "\n";
            Log.d("tech_jsontest", str);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        //对象里有数组
        str = "";
        json = "{\"username\":\"张木生\",age:11,\"jicheng\":[{\"zhengshu\":\"PMP\",\"date\":\"2011年\"},{\"zhengshu\":\"信息系统项目管理师\",\"date\":\"2012年\"}],\"addr\":\"江西\"}";
        try {
            jsonObject = new JSONObject(json);
            str = "名字:" + jsonObject.getString("username") + " 年龄:" + jsonObject.getString("age");
            JSONArray jichengArray = jsonObject.getJSONArray("jicheng");
            for (int i = 0; i < jichengArray.length(); i++) {
                JSONObject jsonJichengObj = (JSONObject) jichengArray.opt(i);
                str += " 级称" + String.valueOf(i + 1) + ": 证书:" + jsonJichengObj.getString("zhengshu")
                        + " 时间:" + jsonJichengObj.get("date") + "\n";
            }
            str += " 地点:" + jsonObject.get("addr") + "\n";
            Log.d("tech_jsontest", str);
        } catch (JSONException e) {
            Log.e("tech_jsontest", e.getMessage());
        }


        //直接一个数组
        str = "";
        json="[{\"devid\":\"1234567800\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\"},{\"devid\":\"1234567832\",\"latitude\":\"29.4943\",\"longitude\":\"1161.129\"}]";
        try {
            JSONArray jsonArrDevid = new JSONArray(json);
            for (int i = 0; i < jsonArrDevid.length(); i++) {
                JSONObject jsonObjDevid = (JSONObject) jsonArrDevid.get(i);
                str += "devid" + i + ": " + "编号:" + jsonObjDevid.getString("devid") + " 精度:"
                        + jsonObjDevid.getString("latitude") + " 纬度:" + jsonObjDevid.get("longitude")+
                         "\n";
            }
            Log.d("tech_jsontest", str);
        } catch (JSONException e) {
            Log.e("tech_jsontest", e.getMessage());
        }


        //数组里有数组
        str = "";
        json="["+
                "{\"devid\":\"1234567800\",\"gps\":[{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\"},{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\" }],\"devname\":\"赣01\"},"+
                " {\"devid\":\"1234567800\",\"gps\":[{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\"},{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\" }],\"devname\":\"赣92\"},"+
                " {\"devid\":\"1234567800\",\"gps\":[{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\"},{\"time\":\"2014-11-12\",\"latitude\":\"29.4963\",\"longitude\":\"116.189\" }],\"devname\":\"赣43\"}"+
                "]";
        try {
            JSONArray jsonArrdevid = new JSONArray(json);
            for (int i = 0; i < jsonArrdevid.length(); i++) {
                JSONObject jsonObjdevid = (JSONObject) jsonArrdevid.get(i);
                str += "devid" + i + ": 编号:" + jsonObjdevid.get("devid");
                JSONArray jsonArrGps = jsonObjdevid.getJSONArray("gps");
                for (int j = 0; j < jsonArrGps.length(); j++) {
                    JSONObject jsonObjGps = (JSONObject) jsonArrGps.get(j);
                    str += " gps" + j + ": 时间:" + jsonObjGps.get("time")+
                           " 经度:" + jsonObjGps.getString("latitude") +
                           " 纬度:" + jsonObjGps.getString("longitude");
                }
                str += " 名称:" + jsonObjdevid.getString("devname") + "\n";
            }
            Log.d("tech_jsontest",str);

        } catch (JSONException e) {
            Log.e("tech_jsontest", e.getMessage());
        }


    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:838783次
    • 积分:8095
    • 等级:
    • 排名:第2479名
    • 原创:60篇
    • 转载:210篇
    • 译文:0篇
    • 评论:76条
    文章分类
    最新评论