`

google-gson对json的解析

 
阅读更多

 

 

代码如下, json格式贴在了代码中,可以拿出来放在txt里通过流方式获取:

 

package com.xuele.bigdata.hiveudf;
import java.io.File;
import java.io.FileReader;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class ContentTest {

	public static void main(String[] args) {
		
		 JsonParser parse =new JsonParser();  //创建json解析器
		 try {
			 JsonObject json = (JsonObject)parse.parse(new FileReader(new File("D:/javajson/json3.txt")));
			 //System.out.println(json.get("coursewaresName"));
			 
			 /*JsonObject result=json.get("result").getAsJsonObject();
	         JsonObject today=result.get("today").getAsJsonObject();
	         //System.out.println("temperature:"+today.get("temperature").getAsString());
	         JsonObject weather_id=today.get("weather_id").getAsJsonObject();
	         System.out.println(weather_id.get("fa"));*/
			 
			 
			 // 在线json格式链接 : https://www.json.cn/#
			 // 使用 google.gson包下的类,  获取元素json对象和获取jsonarray数组两个方法 
			 
			/* {
				    "bgID":null,
				    "coursewaresName":"2. 玩滑梯的启示20170321074758",
				    "pages":[
				        [
				            {
				                "text":"摩擦力的大小与什么因素有关系",
				                "x":2,
				                "type":2,
				                "y":10,
				                "isOpen":0,
				                "index":-1,
				                "place":0,
				                "property":{
				                    "rotation":null,
				                    "ex":"jpg",
				                    "smallImg":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/98x98_7D914DAF76A9FF81BA5F693F5EBC3D34.jpg?t=20160310",
				                    "studentName":"",
				                    "code":"7D914DAF76A9FF81BA5F693F5EBC3D34",
				                    "path":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/7D914DAF76A9FF81BA5F693F5EBC3D34.jpg?t=20160310",
				                    "width":60,
				                    "from":2,
				                    "fileType":null,
				                    "height":45
				                }
				            },
				            {
				                "text":"摩擦力与我们的生活有什么关系",
				                "x":2,
				                "type":2,
				                "y":65,
				                "isOpen":0,
				                "index":-1,
				                "place":1,
				                "property":{
				                    "rotation":null,
				                    "ex":"jpg",
				                    "smallImg":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/98x98_144887EAF7397EC332FAEE11180766C9.jpg?t=20160310",
				                    "studentName":"",
				                    "code":"144887EAF7397EC332FAEE11180766C9",
				                    "path":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/144887EAF7397EC332FAEE11180766C9.jpg?t=20160310",
				                    "width":60,
				                    "from":2,
				                    "fileType":null,
				                    "height":45
				                }
				            },
				            {
				                "text":"(青岛版)三年级科学下册练习题 玩滑梯的启示 1 ",
				                "x":2,
				                "type":1,
				                "y":120,
				                "isOpen":0,
				                "index":-1,
				                "place":2,
				                "property":{
				                    "rotation":null,
				                    "ex":"doc",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"d190c429855cb6896e0a64057fe25859",
				                    "path":"",
				                    "width":60,
				                    "from":7,
				                    "fileType":"5",
				                    "height":45
				                }
				            },
				            {
				                "text":"《玩滑梯的启示》习题",
				                "x":2,
				                "type":1,
				                "y":175,
				                "isOpen":0,
				                "index":-1,
				                "place":3,
				                "property":{
				                    "rotation":0,
				                    "ex":"doc",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"4fbcf7baa43f7f9128f4d05a13c2ddf5",
				                    "path":"",
				                    "width":60,
				                    "from":7,
				                    "fileType":"4",
				                    "height":45
				                }
				            }
				        ],
				        [
				            {
				                "text":"第5页",
				                "x":2,
				                "type":2,
				                "y":10,
				                "isOpen":0,
				                "index":-1,
				                "place":0,
				                "property":{
				                    "rotation":0,
				                    "ex":"jpg",
				                    "smallImg":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/98x98_6273CBD9335AEF99683EDAD44C708008.jpg?t=20160310",
				                    "studentName":"",
				                    "code":"6273CBD9335AEF99683EDAD44C708008",
				                    "path":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/6273CBD9335AEF99683EDAD44C708008.jpg?t=20160310",
				                    "width":60,
				                    "from":2,
				                    "fileType":null,
				                    "height":45
				                }
				            },
				            {
				                "text":"滑梯里有哪些学问",
				                "x":2,
				                "type":2,
				                "y":65,
				                "isOpen":0,
				                "index":-1,
				                "place":1,
				                "property":{
				                    "rotation":null,
				                    "ex":"jpg",
				                    "smallImg":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/98x98_EF796E20F1099B81169A3AE29E3A59C7.jpg?t=20160310",
				                    "studentName":"",
				                    "code":"EF796E20F1099B81169A3AE29E3A59C7",
				                    "path":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/EF796E20F1099B81169A3AE29E3A59C7.jpg?t=20160310",
				                    "width":60,
				                    "from":2,
				                    "fileType":null,
				                    "height":45
				                }
				            },
				            {
				                "text":"《玩滑梯的启示》图片:滑梯3",
				                "x":2,
				                "type":2,
				                "y":120,
				                "isOpen":0,
				                "index":-1,
				                "place":2,
				                "property":{
				                    "rotation":0,
				                    "ex":"jpg",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"cc9ad9e52d348bbd9e8f3ae3a7a6e6c5",
				                    "path":"",
				                    "width":60,
				                    "from":7,
				                    "fileType":"6",
				                    "height":45
				                }
				            },
				            {
				                "text":"《玩滑梯的启示》课件1",
				                "x":74,
				                "type":1,
				                "y":35,
				                "isOpen":1,
				                "index":1,
				                "place":-1,
				                "property":{
				                    "rotation":0,
				                    "ex":"ppt",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"70b10b8f238c52a7924d6e54f6528804",
				                    "path":"",
				                    "width":870.4,
				                    "from":7,
				                    "fileType":"4",
				                    "height":519.35
				                }
				            },
				            {
				                "text":"《玩滑梯的启示》习题1",
				                "x":2,
				                "type":1,
				                "y":175,
				                "isOpen":0,
				                "index":-1,
				                "place":3,
				                "property":{
				                    "rotation":0,
				                    "ex":"doc",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"4a6e3378abda2a37812d492ddaddc0d4",
				                    "path":"",
				                    "width":60,
				                    "from":7,
				                    "fileType":"5",
				                    "height":45
				                }
				            }
				        ],
				        [
				            {
				                "text":"说一说玩滑梯的感觉",
				                "x":2,
				                "type":2,
				                "y":10,
				                "isOpen":0,
				                "index":-1,
				                "place":0,
				                "property":{
				                    "rotation":0,
				                    "ex":"jpg",
				                    "smallImg":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/98x98_BA5C048D765D06D8452D0E583D184591.jpg?t=20160310",
				                    "studentName":"",
				                    "code":"BA5C048D765D06D8452D0E583D184591",
				                    "path":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/BA5C048D765D06D8452D0E583D184591.jpg?t=20160310",
				                    "width":60,
				                    "from":2,
				                    "fileType":null,
				                    "height":45
				                }
				            },
				            {
				                "text":"摩擦力",
				                "x":2,
				                "type":2,
				                "y":65,
				                "isOpen":0,
				                "index":-1,
				                "place":1,
				                "property":{
				                    "rotation":0,
				                    "ex":"jpg",
				                    "smallImg":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/98x98_600EDD41EEC1A2B55FD4E322C5C13E37.jpg?t=20160310",
				                    "studentName":"",
				                    "code":"600EDD41EEC1A2B55FD4E322C5C13E37",
				                    "path":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/600EDD41EEC1A2B55FD4E322C5C13E37.jpg?t=20160310",
				                    "width":60,
				                    "from":2,
				                    "fileType":null,
				                    "height":45
				                }
				            },
				            {
				                "text":"(青岛版)三年级科学下册教案 玩滑梯的启示 1 ",
				                "x":2,
				                "type":1,
				                "y":120,
				                "isOpen":0,
				                "index":-1,
				                "place":2,
				                "property":{
				                    "rotation":0,
				                    "ex":"doc",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"d39c23ac4a1ddfe09e2caccce9d0bc37",
				                    "path":"",
				                    "width":60,
				                    "from":7,
				                    "fileType":"2",
				                    "height":45
				                }
				            },
				            {
				                "text":"《玩滑梯的启示》教案2",
				                "x":2,
				                "type":1,
				                "y":175,
				                "isOpen":0,
				                "index":-1,
				                "place":3,
				                "property":{
				                    "rotation":null,
				                    "ex":"doc",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"9e2769999fb5d7883751a07b95cddc68",
				                    "path":"",
				                    "width":60,
				                    "from":7,
				                    "fileType":"2",
				                    "height":45
				                }
				            }
				        ],
				        [
				            {
				                "text":"第4页",
				                "x":2,
				                "type":2,
				                "y":10,
				                "isOpen":0,
				                "index":-1,
				                "place":0,
				                "property":{
				                    "rotation":null,
				                    "ex":"jpg",
				                    "smallImg":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/98x98_D596E5B62CE21B63FE3EA08AC7C06F52.jpg?t=20160310",
				                    "studentName":"",
				                    "code":"D596E5B62CE21B63FE3EA08AC7C06F52",
				                    "path":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/D596E5B62CE21B63FE3EA08AC7C06F52.jpg?t=20160310",
				                    "width":60,
				                    "from":2,
				                    "fileType":null,
				                    "height":45
				                }
				            },
				            {
				                "text":"玩滑梯的启示",
				                "x":2,
				                "type":2,
				                "y":65,
				                "isOpen":0,
				                "index":-1,
				                "place":1,
				                "property":{
				                    "rotation":0,
				                    "ex":"jpg",
				                    "smallImg":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/98x98_2CBCB2FE952DA3A6D1F6D388D8B21232.jpg?t=20160310",
				                    "studentName":"",
				                    "code":"2CBCB2FE952DA3A6D1F6D388D8B21232",
				                    "path":"http://ebookfile.xueleyun.com/r/040003002027100/040003002027100001002/2CBCB2FE952DA3A6D1F6D388D8B21232.jpg?t=20160310",
				                    "width":60,
				                    "from":2,
				                    "fileType":null,
				                    "height":45
				                }
				            },
				            {
				                "text":"《玩滑梯的启示》图片:滑梯",
				                "x":2,
				                "type":2,
				                "y":120,
				                "isOpen":0,
				                "index":-1,
				                "place":2,
				                "property":{
				                    "rotation":0,
				                    "ex":"jpg",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"702563eb418ba774feed7a38c4d6b088",
				                    "path":"",
				                    "width":60,
				                    "from":7,
				                    "fileType":"6",
				                    "height":45
				                }
				            },
				            {
				                "text":"《玩滑梯的启示》图片:滑梯3",
				                "x":2,
				                "type":2,
				                "y":175,
				                "isOpen":0,
				                "index":-1,
				                "place":3,
				                "property":{
				                    "rotation":null,
				                    "ex":"jpg",
				                    "smallImg":"",
				                    "studentName":"",
				                    "code":"cc9ad9e52d348bbd9e8f3ae3a7a6e6c5",
				                    "path":"",
				                    "width":60,
				                    "from":7,
				                    "fileType":"6",
				                    "height":45
				                }
				            }
				        ]
				    ],
				    "bgStyle":1
				}*/
			 // 解析 授课神器下的 text 格式素材 
			 JsonArray array=json.get("pages").getAsJsonArray(); // 解析得到第一层数组
			 //System.out.println(array);
			 for(int i=0;i<array.size();i++){
	                System.out.println("---------------");
	                JsonArray array1 = (JsonArray) array.get(i); // 解析得到第二层数组 
	                //System.out.println(array1);
	                for(int j=0;j<array1.size();j++) {
	                	JsonObject subObject=(JsonObject) array1.get(j);
	                	boolean b = subObject.has("text");
	                	System.out.println(b);
	                	// 不断累加即可 
	                	//System.out.println(subObject.get("text"));
	                	
	                }
	            }
			 
			/* {
				    "content":{
				        "id":"26b716bba9614652bdc8c74439baa3a6",
				        "name":"1我们的民族小学20180703",
				        "saveVersion":"1",
				        "isOnline":true,
				        "blackboards":{
				            "items":[
				                {
				                    "elements":{
				                        "itemList":[
				                            Object{...},
				                            {
				                                "colorKey":"#ed2b48",
				                                "config":{
				                                    "width":551.1087267525036,
				                                    "height":811,
				                                    "originalWidth":551.1087267525036,
				                                    "originalHeight":811,
				                                    "translate":{
				                                        "x":837.4456366237482,
				                                        "y":54.5,
				                                        "sx":-275.5543633762518,
				                                        "sy":-405.5,
				                                        "ex":-275.5543633762518,
				                                        "ey":-405.5
				                                    },
				                                    "rotate":0,
				                                    "scale":1
				                                },
				                                "elementType":1,
				                                "localSrc":"",
				                                "onlineFileConfig":{
				                                    "fileKey":"86c8ca5b5c4732828c4c2613082d7642",
				                                    "fileType":6,
				                                    "fileSmallUrl":"https://dl.xueleyun.com/images/98x98_86c8ca5b5c4732828c4c2613082d7642.jpg",
				                                    "fileName":"第2页",
				                                    "extension":"jpg",
				                                    "fileSize":0,
				                                    "isFeedBack":false,
				                                    "isUpload":false,
				                                    "url":"https://dl.xueleyun.com/images/86c8ca5b5c4732828c4c2613082d7642.jpg",
				                                    "pptIndexUrl":"undefined",
				                                    "favoriteId":"",
				                                    "diskId":"218193",
				                                    "userIcon":"",
				                                    "userName":"",
				                                    "thumbCache":"undefined"
				                                },
				                                "isDraw":true,
				                                "isNeedSave":true,
				                                "isPublic":false,
				                                "isBook":true
				                            }
				                        ]
				                    },
				                    "inactiveElements":[

				                    ],
				                    "penLines":{
				                        "itemList":[
				                            {
				                                "colorKey":"#27997e",
				                                "lineWidth":5,
				                                "lineColor":"rgba(255, 255, 255, 1)",
				                                "points":{
				                                    "items":[
				                                        {
				                                            "x":280,
				                                            "y":379
				                                        }
				                                    ]
				                                }
				                            },
				                            {
				                                "colorKey":"#d9297c",
				                                "lineWidth":5,
				                                "lineColor":"rgba(255, 255, 255, 1)",
				                                "points":{
				                                    "items":[
				                                        {
				                                            "x":385,
				                                            "y":688
				                                        },
				                                        {
				                                            "x":534,
				                                            "y":490
				                                        }
				                                    ]
				                                }
				                            },
				                            {
				                                "colorKey":"#dd5771",
				                                "lineWidth":5,
				                                "lineColor":"rgba(255, 255, 255, 1)",
				                                "points":{
				                                    "items":[
				                                        {
				                                            "x":230,
				                                            "y":468
				                                        },
				                                        {
				                                            "x":889,
				                                            "y":420
				                                        }
				                                    ]
				                                }
				                            },
				                            {
				                                "colorKey":"#b50ca5",
				                                "lineWidth":5,
				                                "lineColor":"rgba(255, 255, 255, 1)",
				                                "points":{
				                                    "items":[
				                                        {
				                                            "x":870,
				                                            "y":346
				                                        },
				                                        {
				                                            "x":945,
				                                            "y":583
				                                        }
				                                    ]
				                                }
				                            },
				                            {
				                                "colorKey":"#520fd1",
				                                "lineWidth":5,
				                                "lineColor":"rgba(255, 255, 255, 1)",
				                                "points":{
				                                    "items":[
				                                        {
				                                            "x":763,
				                                            "y":128
				                                        }
				                                    ]
				                                }
				                            }
				                        ]
				                    }
				                }
				            ]
				        }
				    },
				    "unitId":"010003001001001001001",
				    "userId":"36838484",
				    "schoolId":"352947",
				    "coursewareName":"1我们的民族小学20180703"
				}*/
			 // 解析h5 中elementType 格式的素材 
			 JsonObject obj = json.get("content").getAsJsonObject(); // 解析得到第一层数组
			 JsonObject blackboards = obj.get("blackboards").getAsJsonObject();
			 JsonArray items = blackboards.get("items").getAsJsonArray();
			 for(int i=0; i<items.size();i++) {
				 JsonObject subObject = (JsonObject)items.get(i);
				 JsonObject elements = subObject.get("elements").getAsJsonObject();
				 JsonArray itemList = elements.get("itemList").getAsJsonArray();
				 for(int j=0; j<itemList.size();j++) {
					 JsonObject item=(JsonObject)itemList.get(j);
					 System.out.println("元素: " + item);
					 boolean b = item.has("elementType");
					 System.out.println(b);
					 if(b) {
						 String value = item.get("elementType").toString();
						 System.out.println(value);
					 }
				 }
			 }

			 
			 
			 
		 }catch (Exception e) {
			 e.printStackTrace();
		 }
	}
}

 

分享到:
评论

相关推荐

    google-gson-2.2.4.zip

    谷歌的Gson库是Java开发人员用来在JSON和Java对象之间进行序列化和反序列化的强大工具。这个"google-gson-2.2.4.zip"压缩包包含了Gson库的2.2.4版本,这是一个广泛使用的稳定版本,适用于各种项目需求。Gson库允许...

    google-gson-2.2.2

    这个"google-gson-2.2.2"版本是Gson的一个具体发行版,它提供了对XML数据的解析功能。在Java开发中,JSON(JavaScript Object Notation)作为一种轻量级的数据交换格式,被广泛应用于网络服务之间以及客户端与服务器...

    android Zip压缩-删除文件-Gson解析Json

    Google提供的Gson库是Java和Android平台上最流行的Json解析库之一。它可以将Java对象转换为Json字符串,反之亦然。要使用Gson,首先需要在项目中添加Gson依赖。接着,我们可以创建一个Java对象,然后使用Gson将其...

    google-gson-2.2.4

    Gson 2.2.4版本还包含了对JSON解析错误的处理机制,如使用`JsonParser`类可以逐个解析JSON元素,如果在解析过程中遇到问题,它会抛出异常,帮助开发者定位问题。 除此之外,这个版本可能已经修复了之前版本中的一些...

    google-gson-2.1 jar包 + 使用教程.pdf

    谷歌的Gson库是Java开发中的一个强大工具,它允许开发者将Java对象转换为JSON格式的字符串,同时也能将JSON数据解析回等效的Java对象。这个“google-gson-2.1.jar”包包含了Gson库的版本2.1,这是一个稳定且广泛使用...

    google-gson-1.6.jar

    Google-Gson是Google推出的一个Java库,它能够将Java对象转换为JSON格式的字符串,同时也能够将JSON内容解析回等效的Java对象,极大地简化了JSON与Java之间的数据交互。 Google-Gson的核心功能在于提供了一种高效且...

    安卓网站交互JSONxmlWebserviceUPnP相关-gson解析json简单教学.zip

    本教程主要关注如何使用Gson库在Android中解析JSON数据。 Gson是由Google提供的Java库,用于将Java对象转换为JSON字符串,以及将JSON字符串反序列化为Java对象。这极大地简化了JSON与Java对象之间的转换过程。下面...

    google-gson-2.1-release.zip+例子

    谷歌的Gson库是一款强大的Java库,用于将Java对象转换为JSON格式的字符串,反之亦然。这个"google-gson-2.1-release.zip+例子"压缩包包含了Gson库的2.1版本以及相关的示例代码,帮助开发者更好地理解和使用这个库。 ...

    Gson解析json数据

    无论面对简单的键值对还是复杂的JSON结构,Gson都能游刃有余地完成解析任务,使得开发者能更专注于业务逻辑,而不是繁琐的数据转换。通过熟练掌握Gson,我们可以更高效地在Java应用中处理JSON数据。

    安卓网站交互JSONxmlWebserviceUPnP相关-Gson解析Json的冲突文件2016-11-1605-42-19-983.rar

    **Gson**是由Google提供的一个Java库,用于在Java对象和JSON数据之间进行映射。它可以将任何Java对象转换为对应的JSON字符串,也可以将JSON内容解析成等效的Java对象。Gson的强大之处在于它的灵活性和简单性,可以...

    google-gson-2.2.4-release(压缩包含原版压缩包和官网下载地址)

    例如,`Gson().fromJson(jsonString, MyClass.class)`将JSON解析成`MyClass`的实例。 2. **Java对象到JSON的转换**:同样,我们可以将Java对象转换为JSON格式的字符串,用于网络传输或存储。如`new Gson().toJson...

    google-json-gson-master.zip

    2. **JSON到Java对象的反序列化**:反之,Gson也可以将JSON字符串解析成对应的Java对象。这对于接收服务器返回的JSON数据并将其转化为可操作的对象非常有用。 3. **类型适配器(TypeAdapter)**:Gson允许自定义...

    google-gson-2.2.2Jar包

    谷歌的Gson库是一个强大的Java库,用于将Java对象转换为JSON格式的字符串,反之亦然。这个"google-gson-2.2.2Jar包"是Gson库的一个特定版本,版本号为2.2.2。在Java开发中,它被广泛应用于网络数据的传输、存储以及...

    通过Gson解析Json文件

    在Java开发中,处理JSON数据是一项常见的任务,Gson库是由Google提供的一个强大的工具,用于在Java对象和JSON数据之间进行映射。本篇文章将详细阐述如何使用Gson库解析JSON文件,帮助开发者理解并掌握这一技能。 ...

    Google Gson 2.8.4 - 谷歌的Json解析包

    Google Gson是谷歌开发的一款强大的JSON解析库,它允许开发者在Java对象和JSON数据之间进行相互转换。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛应用于Web服务和应用程序之间的数据通信...

    converter-gson-2.9.0.jar / retrofit-2.9.0.jar

    Gson是Google提供的一个开源库,用于在Java对象和JSON数据之间进行映射。在Android开发中,Gson广泛用于将Java对象序列化为JSON字符串,或者将JSON字符串反序列化为Java对象。Gson 2.9.0版本也包含了性能优化和bug...

    Gson解析复杂Json实例,超简单

    本教程将深入探讨如何使用Gson库解析复杂的JSON实例,让你只需寥寥几行代码就能轻松处理JSON解析问题。 ### Gson库简介 Gson库的核心功能是将Java对象(如自定义类)转换为JSON字符串,反之亦然。这个库非常直观,...

Global site tag (gtag.js) - Google Analytics