码迷,mamicode.com
首页 > Web开发 > 详细

Json的生成和解析

时间:2017-04-16 17:37:48      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:字符   ast   context   make   pack   json   final   ue4   cte   

json是常见的数据格式,生成和解析是常用的操作。Android中,默认提供orgJson供我们使用,除此之外,google也提供了Gson库方便我们开发。

Json样例类

package com.fxb.jsontest;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class JsonUtils {

    public static String createJson(){
        try {
            JSONObject json = new JSONObject();
            json.put("key1", "value1");
            json.put("key2", "value2");

            JSONObject json1 = new JSONObject();
            json1.put("key3", "value3");
            json1.put("key4", "value4");
            json.put("child1", json1);

            JSONObject json2 = new JSONObject();
            json2.put("key5", "value5");
            json2.put("key6", "value6");
            json1.put("child2", json2);

            JSONObject json3 = new JSONObject();
            json3.put("key7", "value7");
            json3.put("key8", "value8");
            json2.put("child2", json3);

            JSONArray arr = new JSONArray();
            JSONObject json4 = new JSONObject();
            json4.put("key9", "value9");
            json4.put("key10", "value10");
            arr.put(json4);

            JSONObject json5 = new JSONObject();
            json5.put("key11", "value11");
            json5.put("key12", "value12");
            arr.put(json5);

            json3.put("child3", arr);

//            Log.i("JsonTest", json.toString());
            return json.toString();
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void orgJsonTest(Context context){
        String str = "{\"key1\":\"value1\",\"key2\":\"value2\",\"child1\":{\"key3\":\"value3\",\"key4\":\"value4\",\"child2\":{\"key5\":\"value5\",\"key6\":\"value6\",\"child3\":{\"key7\":\"value7\",\"key8\":\"value8\",\"child4\":[{\"key9\":\"value9\",\"key10\":\"value10\"},{\"key11\":\"value11\",\"key12\":\"value12\"}]}}}}";
        try {
            JSONObject jsonRoot = new JSONObject(str);
            JSONObject jsonChild3 = jsonRoot.getJSONObject("child1").getJSONObject("child2").getJSONObject("child3");
            if(jsonChild3 != null){
                String str7 = jsonChild3.getString("key7");
                String str8 = jsonChild3.getString("key8");

                JSONArray jsonArr = jsonChild3.getJSONArray("child4");
                JSONObject jsonChild4_1 = jsonArr.getJSONObject(0);
                String str9 = jsonChild4_1.getString("key9");
                String str10 = jsonChild4_1.getString("key10");

//                Log.i(MainActivity.TAG, "str7:"+str7+", str8:"+str8);
//                Log.i(MainActivity.TAG, "str9:"+str9+", str10:"+str10);
                String strShow = "str7:"+str7+", str8:"+str8 + "\n"+"str9:"+str9+", str10:"+str10;
                showToast(context, strShow);
            }
            else{
                Log.i(MainActivity.TAG, "empty");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void gsonTest(Context context){
        Gson gson = new Gson();
        int i = gson.fromJson("100", int.class);
        double d = gson.fromJson("1.234", double.class);
        boolean b = gson.fromJson("true", boolean.class);

        final String strPerson = "{\"id\":5678,\"name\":\"Jack\",\"isWell\":\"true\",\"adress\":\"beijing\"}";

        Person person = gson.fromJson(strPerson, Person.class);
        Toast.makeText(context, "i:" + i + "\nd:" + d + "\nb:" + b + "\nperson\n" + person.show(), Toast.LENGTH_SHORT).show();

    }

    public static void showToast(Context context, String str){
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }


    public static class Person{
        private int id;
        private String name;
        private boolean isWell;
        private String adress;

        public String show(){
            return "id="+id+", isWell="+ isWell +", adress="+adress;
        }
    }

}

测试Activity类

package com.fxb.jsontest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

    public static final String TAG = "JsonTest";
    private Button btnCreateJson, btnOrgJson, btnGson, btnFastJson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        btnCreateJson = (Button)findViewById(R.id.btnCreateJson);
        btnOrgJson = (Button)findViewById(R.id.btnOrgJson);
        btnGson = (Button)findViewById(R.id.btnGson);
        btnFastJson = (Button)findViewById(R.id.btnFastJson);

        btnCreateJson.setOnClickListener(this);
        btnOrgJson.setOnClickListener(this);
        btnGson.setOnClickListener(this);
        btnFastJson.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == btnCreateJson){
            String str = JsonUtils.createJson();
            Log.i(TAG, str);
        }
        else if(v == btnOrgJson){
            JsonUtils.orgJsonTest(this);
        }
        else if(v == btnGson){

        }
        else if(v == btnFastJson){

        }
    }
}

  样例中,成功创建了json字符串,并实现了解析。

Json的生成和解析

标签:字符   ast   context   make   pack   json   final   ue4   cte   

原文地址:http://www.cnblogs.com/MiniHouse/p/6719332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!