String JSONObject JSONArray 转换

JSON使用阿里的fastJson为依赖包

gradle依赖管理如下:

    compile group: ‘com.alibaba‘, name: ‘fastjson‘, version:‘1.2.41‘

1、String转JSONObject

前言:String 是JSONObject格式的字符串

eg:

JSONObject jSONObject = JSONObject.parseObject(String);

2、String转JSONArray

前言:String 是JSONArray格式的字符串

eg:

JSONArray jsonArray= JSONArray.parseArray(String);

3、JSONObject中的数组提取为JSONArray

eg:

{
    "AreaName": "北京",
    "CityId": 110100,
    "NoMarket": false,
    "OldCityId": 646,
    "Pinyin": "beijing",
    "ProvinceId": 110000,
    "Result": [
        {
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "Stelvio 钜惠23.4万起",
            "Url": "//www.autohome.com.cn/market/201904/100223763.html"
        },
        {
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "马驹桥林肯中心年中大促",
            "Url": "//www.autohome.com.cn/market/201906/100230932.html"
        },
        {
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "星越平价销售13.58万元起",
            "Url": "//www.autohome.com.cn/dealer/201906/367011492.html"
        },
        {
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "哈弗F5限时优惠8000元",
            "Url": "//www.autohome.com.cn/dealer/201906/366897778.html"
        },
        {
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "购元新能源价格暂无优惠",
            "Url": "//www.autohome.com.cn/dealer/201906/366897034.html"
        },
        {
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "瑞虎3xe冰点价促销中!",
            "Url": "//www.autohome.com.cn/dealer/201906/366889724.html"
        },
        {
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "购奔奔EV现钜惠5.1万元",
            "Url": "//www.autohome.com.cn/dealer/201906/366843204.html"
        },
        {
            "ItemName": "优惠",
            "ItemUrl": "/list/a646c12-1.html",
            "Title": "购宝马7系价格暂无优惠",
            "Url": "//www.autohome.com.cn/dealer/201906/366588080.html"
        },
        {
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "途观L价格直降7.6万元",
            "Url": "//www.autohome.com.cn/dealer/201906/366568937.html"
        },
        {
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "购凯迪拉克XTS降8万",
            "Url": "//www.autohome.com.cn/dealer/201906/366500646.html"
        },
        {
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "汉兰达可试驾购车无优惠",
            "Url": "//www.autohome.com.cn/dealer/201906/366384207.html"
        },
        {
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "宝马M4价格稳定无优惠",
            "Url": "//www.autohome.com.cn/dealer/201906/366156789.html"
        },
        {
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "奥迪A8促销直降26.33万元",
            "Url": "//www.autohome.com.cn/dealer/201906/366925378.html"
        },
        {
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "英菲尼迪Q50L可降6.3万",
            "Url": "//www.autohome.com.cn/dealer/201906/366863516.html"
        },
        {
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "帝豪新能源价格降8.25万",
            "Url": "//www.autohome.com.cn/dealer/201906/366877669.html"
        },
        {
            "ItemName": "预定",
            "ItemUrl": "/list/a646c14-1.html",
            "Title": "撼路者在售现钜惠5万",
            "Url": "//www.autohome.com.cn/dealer/201906/366912121.html"
        }
    ]
}

提取Result对应的数组

JSONArray jsonArray= jsonObject.getJSONArray("Result");

4、JSONArray提取为JSONObject

eg:

JSONObject jsonObject = jsonArray.getJSONObject(0);

5、JSONObject获取value

1、object.getString("key")

2、object.get("key")

6、获取JSONObject的ket value

        JSONArray dateArr = new JSONArray();
        Set<String> key = dateArr .keySet();
        for (String keyObj:key) {
            JSONArray hisData = history.getJSONArray(keyObj);
        }

7、遍历JSONArray

第一种for循环
                JSONArray seriesArr = new JSONArray();
                for(int i=0;i<seriesArr .size();i++){
                    JSONObject object = eggsArr.getJSONObject(i);
                }

第二种for增强
                JSONArray pzListArr = new JSONArray();
                for (Object obj:pzListArr) {
                    JSONObject dataObj = JSONObject.parseObject(obj.toString());
                }    

8、javaBean转为JSONObject

未完待续······

原文地址:https://www.cnblogs.com/ljangle/p/11047111.html

时间: 2024-08-29 12:54:38

String JSONObject JSONArray 转换的相关文章

解决嵌套list类型的JSONObject/JSONArray转换成原来list问题。

解决转换过程中,对象的集合类成员变量变成其他类型对象的问题. public class A { private List<B> blist ; public A(List<B> temp){ blist = temp; } public void setBlist(List<B> temp){ blist = temp; } public List<B> getBlist(){ return blist; } } public class Mainclass

jsonArray转换成List

从字符串String转换成List 字符串格式: String jsonstr = "{'studentsjson':[{'student':'张三'},{'student':'李四'}] }"; List格式: [张三, 李四] 代码如下: public static List StrToList1(String jsonStr) { List<String> list = new ArrayList<String>(); JSONObject jsonObj

android Json 解析 JSONObject JSONArray

android Json 解析  JSONObject JSONArray 我只解析了一个,不管有多少个都以此方法类推,加上http://blog.csdn.net/menglele1314/article/details/46324069里的JSONArray解析,可以应付一般所以的开发了 try { String value = "{\"result\":\"S\", \"address\":[{\"shengfen\&q

String数据类型的转换

数据类型转换各种原始数据类型与String类型之间可以通过方法相互转换.valueOf()系列的静态方法用于从其它对象(原始数据类型对象)转换成字符串.例如:public static String valueOf(Booleanb)public static String valueOf(charc)public static String valueOf(inti)public static String valueOf(longl)public static String valueOf(

3.6 java基础总结①包装类,基本数据类型,String相互间转换

包装类,基本数据类型,String相互间转换 每个基本数据类型都有相对应的包装类型 包装类,基本数据类型,String相互间转换,这三者间的转换时很重要的: 一.String 转其他 String → 包装类型1.Integer(String str)调用包装类的带String参构造 String → 基本数据类型☆String → int调用包装类的parseInt方法 二.包装类转其他 → String调用包装类对象的ToString方法 → 基本类型①调用包装类的XXXValue方法包装类

.NET Enum,Int,String的互相转换

C# Enum,Int,String的互相转换 [转] C# Enum,Int,String的互相转换 Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举. 注意:枚举类型的基类型是除 Char 外的任何整型,所以枚举类型的值是整型值. Enum 提供一些实用的静态方法: (1)比较枚举类的实例的方法 (2)将实例的值转换为其字符串表示形式的方法 (3)将数字的字符串表

char*,string,float,int 转换

char* 转 float: double atof (const char* str); /* atof example: sine calculator */ #include <stdio.h> /* printf, fgets */ #include <stdlib.h> /* atof */ #include <math.h> /* sin */ int main () { double n,m; double pi=3.1415926535; char bu

(转)C# Enum,Int,String的互相转换 枚举转换

(转)C# Enum,Int,String的互相转换 枚举转换 Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举. 注意:枚举类型的基类型是除 Char 外的任何整型,所以枚举类型的值是整型值. Enum 提供一些实用的静态方法: (1)比较枚举类的实例的方法 (2)将实例的值转换为其字符串表示形式的方法 (3)将数字的字符串表示形式转换为此类的实例的方法 (4)创建

C# Enum,Int,String的互相转换 枚举转换

(转)C# Enum,Int,String的互相转换 枚举转换 Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举. 注意:枚举类型的基类型是除 Char 外的任何整型,所以枚举类型的值是整型值. Enum 提供一些实用的静态方法: (1)比较枚举类的实例的方法 (2)将实例的值转换为其字符串表示形式的方法 (3)将数字的字符串表示形式转换为此类的实例的方法 (4)创建