Json的生成和解析
json是常见的数据格式,生成和解析是常用的操作。Android中,默认提供orgJson供我们使用,除此之外,google也提供了Gson库方便我们开发。
Json样例类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 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类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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字符串,并实现了解析。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!