json生成与解析

屌丝迷途      2022-02-07     463

关键词:

 

JSON常用与服务器进行数据交互,JSON中“{}”表示JSONObject,“[]”表示JSONArray

如下json数据:

1  {"singers":[
2 {"id":"02","name":"tom","gender":"男","tel":["123456","789012"]},
3 {"id":"03","name":"jerry","gender":"男","tel":["899999","666666"]},
4 {"id":"04","name":"jim","gender":"男","tel":["7777","5555"]},{"id":"05","name":"lily","gender":"女","tel":["222222","111111"]}
5 ]}

 

生成json数据代码:

 1 public String buildJson() throws JSONException {
 2 
 3         JSONObject persons = new JSONObject();
 4 
 5         JSONArray personArr = new JSONArray();
 6 
 7         JSONObject person = new JSONObject();
 8         person.put("id", "02");
 9         person.put("name", "tom");
10         person.put("gender", "男");
11 
12         JSONArray tel = new JSONArray();
13         tel.put("123456");
14         tel.put("789012");
15 
16         person.put("tel", tel);
17 
18         personArr.put(person);
19 
20         JSONObject person2 = new JSONObject();
21         person2.put("id", "03");
22         person2.put("name", "jerry");
23         person2.put("gender", "男");
24 
25         JSONArray tel2 = new JSONArray();
26         tel2.put("899999");
27         tel2.put("666666");
28 
29         person2.put("tel", tel2);
30 
31         personArr.put(person2);
32 
33 
34         JSONObject person3 = new JSONObject();
35         person3.put("id", "04");
36         person3.put("name", "jim");
37         person3.put("gender", "男");
38 
39         JSONArray tel3 = new JSONArray();
40         tel3.put("7777");
41         tel3.put("5555");
42 
43         person3.put("tel", tel3);
44 
45         personArr.put(person3);
46 
47 
48         JSONObject person4 = new JSONObject();
49         person4.put("id", "05");
50         person4.put("name", "lily");
51         person4.put("gender", "女");
52 
53         JSONArray tel4 = new JSONArray();
54         tel4.put("222222");
55         tel4.put("111111");
56 
57         person4.put("tel", tel4);
58 
59         personArr.put(person4);
60 
61 
62         persons.put("singers", personArr);
63 
64 
65         return persons.toString();
66     }

 

解析json数据代码:

 1  private void parseJsonMulti(String strResult) {
 2         try {
 3             JSONArray jsonObjs = new JSONObject(strResult).getJSONArray("singers");
 4             String s = "";
 5 
 6             for (int i = 0; i < jsonObjs.length(); i++) {
 7                 JSONObject jsonObj = ((JSONObject) jsonObjs.opt(i));
 8                 int id = jsonObj.getInt("id");
 9                 String name = jsonObj.getString("name");
10                 String gender = jsonObj.getString("gender");
11                 s += "ID号" + id + ", 姓名:" + name + ",性别:" + gender + ",电话:";
12                 JSONArray tel = jsonObj.getJSONArray("tel");
13                 for (int j = 0; j < tel.length(); j++) {
14 
15                     s += tel.getString(j)+"/";
16                 }
17 
18                 s += "
";
19 
20             }
21             tv.setText(s);
22         } catch (JSONException e) {
23             e.printStackTrace();
24         }
25     }

 

ios开发之json格式数据的生成与解析

 我们为什么要用JSON格式的数据?JSON格式取代了xml给网络传输带来了很大的便利,但是却没有了xml的一目了然,尤其是json数据很长的时候,我们会陷入繁琐复杂的数据节点查找中。这时我们就需要一款在线校验工具BeJson。一、JSO... 查看详情

何时在 Ruby 的 JSON 库中使用转储与生成与 to_json 以及加载与解析?

】何时在Ruby的JSON库中使用转储与生成与to_json以及加载与解析?【英文标题】:Whentousedumpvs.generatevs.to_jsonandloadvs.parseinRuby\'sJSONlib?【发布时间】:2014-03-1610:46:22【问题描述】:david4dev对thisquestion的回答声称使用json库可以通过三... 查看详情

android配置文件分享和json数据生成与解析

...这里大体是讲的一个关于“Android配置文件分享和JSON数据生成与解析”的整体流程,具体数据库中的数据根据读者自己的项目来安排,如果您看不大懂也请您原谅,毕竟我说了,我只是新手。其实关于数据库中的数据你只需要知... 查看详情

geojson的生成与解析,json解析,java读写geojson,geotools读取shp文件,geotools中geometry对象与geojson的相互转换(代码片段)

GeoJson的生成与解析一、wkt格式的geometry转成json格式二、json格式转wkt格式三、json格式的数据进行解析四、Java读写geojson五、geotools读取shp文件5.1pom.xml5.2读取shp文件六、Geotools中Geometry对象与GeoJson的相互转换6.1pom.xmlimport包6.2LineString... 查看详情

2go语言json与xml解析与表单操作(代码片段)

...N序列化2.2JSON反序列化2.3解析到interface3XML方式3.1解析XML3.2生成XML4字段校验5文件上传2.1前后端模拟上传2.2go客户端模拟上传3防止重复提交1数据交互的格式常见的数据交互格式有:JSON:JavaScriptObjectNotation࿰ 查看详情

2go语言json与xml解析与表单操作(代码片段)

...N序列化2.2JSON反序列化2.3解析到interface3XML方式3.1解析XML3.2生成XML4字段校验5文件上传2.1前后端模拟上传2.2go客户端模拟上传3防止重复提交1数据交互的格式常见的数据交互格式有:JSON:JavaScriptObjectNotation࿰ 查看详情

json与jsonpath

...据交换格式,因为它良好的可读性与易于机器进行解析和生成等特性,在当前的数据整理和收集中得到了广泛的应用。JSON和XML相比较可谓不相上下。Python2.X中自带了JSON模块,直接importjson就可以使用了。官方文档:http://docs.python... 查看详情

json的生成和解析

json是常见的数据格式,生成和解析是常用的操作。Android中,默认提供orgJson供我们使用,除此之外,google也提供了Gson库方便我们开发。Json样例类packagecom.fxb.jsontest;importandroid.content.Context;importandroid.util.Log;importandroid.widget.Toast;impor... 查看详情

使用gson解析,生成json

包:gson-2.3.jarJson文本解析为Java对象;Java对象生成为Json文本importcom.google.gson.Gson;publicclassTestGson{ classPerson{ privateStringname; privateint[]power; privateHorsehs1; privateHorse[]hs2; publicHorsegetH 查看详情

c#解析json

...语法很简单,易于人阅读和编写,同时也易于机器解析和生成。JSON与XML的比较◆可读性  JSON和XML的可读性相比较而言,由于XML提供辅助的标签, 查看详情

使用jsonobject生成和解析json

使用JSONObject生成和解析json1.json数据类型类型描述Number数字型String字符串型Boolean布尔型Array数组Object对象null空值(1)json中不区分整数、小数等类型,而统一使用Number来存储数字。(2)Array表示数组,以中括号"[]"括起来,元素之... 查看详情

2go语言json与xml解析与表单操作(代码片段)

...N序列化2.2JSON反序列化2.3解析到interface3XML方式3.1解析XML3.2生成XML4字段校验5文件上传2.1前后端模拟上传2.2go客户端模拟上传3防止重复提交1数据交互的格式常见的数据交互格式有:JSON:JavaScriptObjectNotation,轻量级的数... 查看详情

6-4json解析与复杂模型转换实用技巧

...方式来进行调用了。数据量大的情况下新建一个test.dart把生成的代码复制过来然后可以根据需要去修改类名这样就可以将这样一个负责的json数据转换成dart里面的对象结束  查看详情

json4:使用json-lib解析生成json

特征:1.包多2.JSONObject.fromObjectimportnet.sf.json.JSONObject;publicclassJsonLib{publicstaticvoidmain(String[]args){Bean2Json();Json2Bean();}staticprivatevoidBean2Json(){Peoplep=newPeople();p.setName("金公 查看详情

java基础-json解析(代码片段)

...JavaScript,但目前很多编程语言都支持JSON格式数据的生成和解析。JSON的官方MIME类型是application/json,文件扩展名是.json数据格式JSON对象"ID":1,"name":"ocean 查看详情

golang生成json及解析json

...可包含多个对象:JSON布尔值二、JSON转map输出结果:三、生成JSON本文来自php中文网的golang教程栏目:https://www.php.cn/be/go/ 查看详情

4-8《xml与json》——xml解析xmlsaxdom4jxstreamjsongsonfastjson(代码片段)

...nt2.元素对象Element3.DOM4J解析XML案例4.DOM4J-XPATH解析XML4、Java生成XML 查看详情

一个简单的json生成/解析库

这是一个单文件的,适用于C语言的,JSON读写库。先说明,不想造轮子,代码是从这里拿来的:https://www.codeproject.com/Articles/887604/jWrite-a-really-simple-JSON-writer-in-Chttps://www.codeproject.com/Articles/885389/jRead-an-in-place-JSON-eleme 查看详情