Json的產生和解析

來源:互聯網
上載者:User

標籤:字元   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的產生和解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.