`

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();
		 }
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics