카테고리 없음

[JSON 파싱]json

9D4U 2020. 10. 15. 15:51
728x90
반응형

메이븐 사용 시,

메이븐 레포지토리에서 json(id - org.json / version - 20200518)을 가져옵니다.

 

예시) JSON 파일에서 키 및 값 출력

 

String content;
		try {
			content = new String(Files.readAllBytes(Paths.get("파일 경로")));
			JSONObject o1 = new JSONObject(content); 
			//System.out.println(content); //json 파일 내용
			
			Iterator<String> iiter = o1.keys();					
			String key = "";
			while (iiter.hasNext()) {
				key= iiter.next();
				System.out.println("1st key : " + key);
				//System.out.println(o1.get(key));
				//System.out.println(o1.get(key).getClass().getName()); // 자료형, 클래스 확인
				
				if(o1.get(key).getClass().getName().equals("org.json.JSONObject")) {
					//System.out.println(o1.getJSONObject(key));
					//System.out.println(o1.getJSONObject(key).length()); // 2: maths, sport
					
					
					JSONObject o2 = o1.getJSONObject(key);
					Iterator<String> iiter2 = o2.keys();
					String key2 = "";
					while (iiter2.hasNext()) {
									key2 = iiter2.next();
									System.out.println("2nd key : "+key2);
									//System.out.println(o2.getJSONObject(key2));
									//System.out.println(o2.getJSONObject(key2).length());
									//System.out.println(o2.get(key2).getClass().getName());
									if(o2.get(key2).getClass().getName().equals("org.json.JSONObject")) {
										JSONObject o3 = o2.getJSONObject(key2);
										Iterator<String> iiter3 = o3.keys();
										String key3 = "";
										while (iiter3.hasNext()) {
											key3 = iiter3.next();
											System.out.println("3rd key : " + key3);
											//System.out.println(o3.getJSONObject(key3));
											//System.out.println(o3.getJSONObject(key3).length());
											//System.out.println(o3.get(key3).getClass().getName());
											if(o3.get(key3).getClass().getName().equals("org.json.JSONObject")) {
												//System.out.println("4th JSONObject");
												JSONObject o4 = o3.getJSONObject(key3);
												Iterator<String> iiter4 = o4.keys();
												String key4 ="";
												while (iiter4.hasNext()) {
													key4 = iiter4.next();
													System.out.println("4th key : " + key4);
													//System.out.println(o4.get(key4).getClass().getName());
													if(o4.get(key4).getClass().getName().equals("org.json.JSONObject")) {
														/*최종 저장 : 더 이상 jsonobject 유형이 나오지 않는 시점*/
														System.out.println("5th jsonobject");
														
													}else if(o4.get(key4).getClass().getName().equals("org.json.JSONArray")) {
														//Array
														JSONArray Jarray = o4.getJSONArray(key4);
														//System.out.println(Jarray.length()); //3
														for (int i = 0; i < Jarray.length(); i++) {
															System.out.println("4th array value"+i+" : " + Jarray.get(i));
														}
													}else {
														System.out.println("4th value : " + o4.get(key4));
													}
													
												}
												
												
												
											}else if(o3.get(key3).getClass().getName().equals("org.json.JSONArray")) {
												//Array
												JSONArray Jarray = o3.getJSONArray(key3);
												//System.out.println(Jarray.length()); //3
												for (int i = 0; i < Jarray.length(); i++) {
													System.out.println("3rd array value"+i+" : " + Jarray.get(i));
												}
											}else {
												System.out.println("3rd value : " + o3.get(key3));
											}
											
										}
									}else if(o2.get(key2).getClass().getName().equals("org.json.JSONArray")) {
										//Array
										JSONArray Jarray = o2.getJSONArray(key2);
										//System.out.println(Jarray.length()); //3
										for (int i = 0; i < Jarray.length(); i++) {
											System.out.println("2nd array value"+i+" : " + Jarray.get(i));
										}
									}else {
										System.out.println("2nd value : " + o2.get(key2));
									}
									
									
													
					}
				}else if(o1.get(key).getClass().getName().equals("org.json.JSONArray")){
					//Array
					JSONArray Jarray = o1.getJSONArray(key);
					//System.out.println(Jarray.length()); //3
					for (int i = 0; i < Jarray.length(); i++) {
						System.out.println("1st array value"+i+" : " + Jarray.get(i));
					}
					
				}else {
					//String ETC
					System.out.println("1st value :" + o1.get(key));
				}									
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
728x90