原创 Android 生成json字符串

2023-8-4 17:05 908 3 3 分类: 智能手机 文集: 技术
String生成的json数据有两种

1

  1. String json="[{'name':'tom','age':12},{'name':'jack','age':13}]";
  2. JSONArray a = new JSONArray(json);
  3. System.out.println(a.toString());

2

  1. String json2="{'json':[{'name':'tom','age':14},{'name':'jack','age':15}]}";
  2. JSONObject b= new JSONObject(json2);
  3. System.out.println(b.toString());

String类型的数据加“”,然后每个key,value加单引号。

JsonObject生成的数据有两种

1

  1. JSONArray array =new JSONArray();
  2. JSONObject object =new JSONObject();
  3. JSONObject object1 =new JSONObject();
  4. JSONObject obj= new JSONObject();
  5. try {
  6. object.put("item1","value1");
  7. object.put("age",12);
  8. object.put("name","tom");
  9. object1.put("item2","value2");
  10. object1.put("age",12232);
  11. object1.put("name","tom");
  12. array.put(object);
  13. array.put(object1);
  14. obj.put("name",array);
  15. System.out.println(obj.toString());
  16. }catch (Exception e){
  17. }
  18. 结果:{"name":[{"item1":"value1","name":"tom","age":12},{"item2":"value2","name":"tom","age":12232}]}

2

  1. JSONArray array1 =new JSONArray();
  2. JSONObject object2 =new JSONObject();
  3. JSONObject object3 =new JSONObject();
  4. try {
  5. object2.put("color","red");
  6. object2.put("height",20);
  7. object3.put("color","blue");
  8. object3.put("height",1010);
  9. array1.put(object2);
  10. array1.put(object3);
  11. System.out.println(array1.toString());
  12. }catch (Exception e){
  13. }
  14. 结果:[{"color":"red","height":20},{"color":"blue","height":1010}]
将集合生成json数据

1

  1. Map<String ,String> map =new HashMap<>();
  2. Map<String ,String> map2 =new HashMap<>();
  3. map.put("name1","tom1");
  4. map.put("age1","12");
  5. map2.put("name1","tom1");
  6. map2.put("age1","12");
  7. JSONObject object4 =new JSONObject();
  8. JSONArray array2 =new JSONArray();
  9. array2.put(map);
  10. array2.put(map2);
  11. object4.put("key",array2);
  12. System.out.println(object4.toString());
  13. 结果:{"key":[{"name1":"tom1","age1":"12"},{"name1":"tom1","age1":"12"}]}

2

  1. Map<String ,String> map1 =new HashMap<>();
  2. map1.put("as","adasd");
  3. map1.put("asfa","afasff");
  4. JSONArray array3 =new JSONArray();
  5. array3.put(map1);
  6. System.out.println(array3.toString());
  7. 结果:[{"asfa":"afasff","as":"adasd"}]
通过Gson生成json

一、单个对象生成json

生成以下类,该怎么生成呢?

1

2

3

4

5

6

{

    "createDate": "2015-02-01 10:39:50", 

    "id": "1", 

    "name": "传说之美", 

    "password": "123456"

}

先定义一个account类,属性有id、name、password、createDate。

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

public class Account {

    private String id;

    private String password;

    private String name;

    private String createDate;

    public Account() {

        super();

    }

    public Account(String id, String password, String name, String createDate) {

        super();

        this.id = id;

        this.password = password;

        this.name = name;

        this.createDate = createDate;

    }

    public String getId() {

        return id;

    }

    public void setId(String id) {

        this.id = id;

    }

    public String getPassword() {

        return password;

    }

    public void setPassword(String password) {

        this.password = password;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getCreateDate() {

        return createDate;

    }

    public void setCreateDate(String createDate) {

        this.createDate = createDate;

    }

    @Override

    public String toString() {

        return "Account [id=" + id + ", password=" + password + ", name=" + name + ", createDate=" + createDate + "]\n\n";

    }

}

定义好这个类,就可以利用Gson生成json字符串了。

1

2

3

4

5

6

7

8

// 生成account对象

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

Account account = new Account("1", "123456", "传说之美", sdf.format(new Date()));

 

// 利用gson对象生成json字符串

Gson gson = new Gson();

String jsonString = gson.toJson(account);

        Log.i("", jsonString);

输入的log如下

 二、解析json字符串单个对象

 在上面已生成了jsonString,那如何将其解析为单个对象,很简单。

1

2

3

// 利用gson解析json字符串为单个对象

Account account1 = gson.fromJson(jsonString, Account.class);

Log.i("", account1.toString());

看看输出的log

三、生成单个对象的json数组

什么事json数组,类似下面的

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[

    {

        "id": "2", 

        "createDate": "2015-02-01 11:21:27", 

        "password": "123456", 

        "name": "传说"

    }, 

    {

        "id": "2", 

        "createDate": "2015-02-01 11:21:27", 

        "password": "123456", 

        "name": "之美"

    }

]

生成json数组代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Account account2 = new Account("2", "123456", "传说", sdf.format(new Date()));

Account account3 = new Account("2", "123456", "之美", sdf.format(new Date()));

List accountList = new ArrayList();

accountList.add(account2);

accountList.add(account3);

 

 

JSONArray accountArray = new JSONArray();

for (int i = 0; i < accountList.size(); i++) {

    String accountStr = gson.toJson(accountList.get(i));

    JSONObject accountObject;

    try {

        accountObject = new JSONObject(accountStr);

        accountArray.put(i, accountObject);

    } catch (JSONException e) {

        e.printStackTrace();

    }

}

Log.i("", accountArray.toString());

log的输出为

四、由多个单个对象的json数组解析为对个单个对象

多个单个对象组成的json数组解析如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

// 解析json数组

List accountList2 = new ArrayList();

for(int i=0;i

    JSONObject jsonObject = null;

    try {

        jsonObject = accountArray.getJSONObject(i);

    } catch (JSONException e) {

        e.printStackTrace();

    }

    if(jsonObject != null){

        Account tempAccount = gson.fromJson(jsonObject.toString(), Account.class);

        accountList2.add(tempAccount);

    }

}

Log.i("accountList2", accountList2.toString());

输出的log

 

或者用更快捷的转化方法

1

2

3

4

5

6

Account[] accountArrays = new Gson().fromJson(accountArray.toString(), Account[].class);

for(int i=0;i

    Log.i("accountArrays",accountArrays.toString());

}

// 转化为List

List foosList = Arrays.asList(accountArrays);

更快捷地解析成List

1

2

3

4

// 更快捷地解析成List

Type listType = new TypeToken>(){}.getType();

ArrayList accsList = new Gson().fromJson(accountArray.toString(), listType);

Log.i("accsList",accsList.toString());

五、生成一个对象嵌套对象的json

嵌套的json类似如下

1

2

3

4

5

6

7

8

9

10

{

    "member": {

        "id": "4", 

        "name": "我是传说"

    }, 

    "id": "4", 

    "createDate": "2015-02-02 12:03:32", 

    "password": "888888", 

    "name": "传说之美"

}

生成这个json有2种方法。

1、再添加一个member类,像添加account类一样添加即可。

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

public class Member {

    private String id;

    private String name;

    public Member() {

        super();

    }

    public Member(String id, String name) {

        super();

        this.id = id;

        this.name = name;

    }

    public String getId() {

        return id;

    }

    public void setId(String id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String toString() {

        return "Member [id=" + id + ", name=" + name + "]\n\n";

    }

}

生成代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

// 生成对象嵌套对象的json

Account account4 = new Account("4", "888888", "传说之美", sdf.format(new Date()));

Member member = new Member("4", "我是传说");

String accountStr = gson.toJson(account4);

String memberStr = gson.toJson(member);

JSONObject object = null;

try {

    JSONObject memberObject = new JSONObject(memberStr);

    object = new JSONObject(accountStr);

    object.put("member", memberObject);     

} catch (JSONException e) {

    e.printStackTrace();

}

Log.i("", object.toString());

输出的log

六、解析对象嵌套对象的json 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Account account5 = gson.fromJson(object.toString(), Account.class);

Log.i("解析对象嵌套对象的json", account5.toString());

// 这里用isNull来判断是否存在这个object,存在就拿出member的JSONObject

JSONObject memberObject = null;

if(!object.isNull("member")){

    try {

        memberObject = object.getJSONObject("member");

    } catch (JSONException e) {

        e.printStackTrace();

    }

}

Member member5 = null;

if(null != memberObject){

    member5 = gson.fromJson(memberObject.toString(), Member.class);

    Log.i("解析对象嵌套对象的json", member5.toString());

}

输出的结果

 7、另外一种解析对象嵌套对象的json 

定义一个类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class AccountObject {

    private String id;

    private String password;

    private String name;

    private String createDate;

    private MemberObject memberObject = new MemberObject();

     

    public class MemberObject {

        private String id;

        private String name;

        @Override

        public String toString() {

            return "MemberObject [id=" + id + ", name=" + name+ "]\n\n";

        }

    }

    @Override

    public String toString() {

        return "AccountObject [id=" + id + ", password=" + password + ", name=" + name + ", createDate=" + createDate + ","+ memberObject.toString() + "]\n\n";

    }

}

生成json并解析

1

2

3

4

5

6

7

8

9

try {

    JSONObject mObject = new JSONObject(memberStr);

    object = new JSONObject(accountStr);

    object.put("memberObject", mObject);        

} catch (JSONException e) {

    e.printStackTrace();

}

AccountObject accountObject = gson.fromJson(object.toString(), AccountObject.class);

Log.i("accountObject", accountObject.toString());

文章评论0条评论)

登录后参与讨论
我要评论
0
3
关闭 站长推荐上一条 /2 下一条