مهدی عادلی فر
بنیانگذار توسینسو و برنامه نویس

آموزش تبدیل داده JSON به داده معمولی در جاوا به زبان ساده

چگونه داده های json را به داده های معمولی در جاوا تبدیل کنیم ؟ در نکته قبلی نحوه تبدیل داده داده های معمولی به json را توضیح دادیم. به عمل تبدیل داده معمولی به json انکود و تبدیل json به داده معمولی را دیکد می گوییم. در این مطلب قصد داریم که دیکد فایل json را توضیح دهیم. برای این حالت یک فایل را در نظر بگیرید که به صورت json ذخیره شده است. حال می خواهیم آن فایل را خوانده و داده های اصلی را از آن به دست آوریم. برای تبدیل فایل json به داده واقعی باید از ساختار داده های ذخیره شده با خبر باشیم. برای مثال فایل json زیر را در نظر بگیرید :

دوره های شبکه، برنامه نویسی، مجازی سازی، امنیت، نفوذ و ... با برترین های ایران
{
	"id": 0001,
	"type": "donut",
	"name": "Cake",
	"ppu": 0.55,
	"batters":
		[
			{ "id": 1001, "type": "Regular" },
			{ "id": 1002, "type": "Chocolate" },
			{ "id": 1003, "type": "Blueberry" },
			{ "id": 1004, "type": "Devil's Food" }
		],
	"topping":
		[
			{ "id": 5001, "type": "None" },
			{ "id": 5002, "type": "Glazed" },
			{ "id": 5005, "type": "Sugar" },
			{ "id": 5007, "type": "Powdered Sugar" },
			{ "id": 5006, "type": "Chocolate with Sprinkles" },
			{ "id": 5003, "type": "Chocolate" },
			{ "id": 5004, "type": "Maple" }
		]
}

برای خواندن این فایل و تبدیل داده های آن از کد زیر استفاده می کنیم.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonParseTest {

	private static final String filePath = "e:\\file.json";
	
	public static void main(String[] args) {

		try {
			// read the json file
			FileReader reader = new FileReader(filePath);
			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject)jsonParser.parse(reader);
			
			// get a number from the JSON object
			Long id =  (Long) jsonObject.get("id");
			System.out.println("The id is: " + id);			

			// get a String from the JSON object
			String	type = (String) jsonObject.get("type");
			System.out.println("The type is: " + type);

			// get a String from the JSON object
			String	name = (String) jsonObject.get("name");
			System.out.println("The name is: " + name);

			// get a number from the JSON object
			Double ppu =  (Double) jsonObject.get("ppu");
			System.out.println("The PPU is: " + ppu);
			
			// get an array from the JSON object
			System.out.println("Batters:");
			JSONArray batterArray= (JSONArray) jsonObject.get("batters");
			Iterator i = batterArray.iterator();
			// take each value from the json array separately
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				System.out.println("ID "+ innerObj.get("id") + 
						" type " + innerObj.get("type"));
			}

			// get an array from the JSON object
			System.out.println("Topping:");
			JSONArray toppingArray= (JSONArray) jsonObject.get("topping");
			Iterator j = toppingArray.iterator();
			// take each value from the json array separately
			while (j.hasNext()) {
				JSONObject innerObj = (JSONObject) j.next();
				System.out.println("ID "+ innerObj.get("id") + 
						" type " + innerObj.get("type"));
			}
			

		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ParseException ex) {
			ex.printStackTrace();
		} catch (NullPointerException ex) {
			ex.printStackTrace();
		}

	}

}

خروجی کد بالا شکل زیر خواهد بود

The id is: 1
The type is: donut
The name is: Cake
The PPU is: 0.55
Batters:
ID 1001 type Regular
ID 1002 type Chocolate
ID 1003 type Blueberry
ID 1004 type Devil's Food
Topping:
ID 5001 type None
ID 5002 type Glazed
ID 5005 type Sugar
ID 5007 type Powdered Sugar
ID 5006 type Chocolate with Sprinkles
ID 5003 type Chocolate
ID 5004 type Maple

قسمت مهم در این کد شئ JSONObject و کلاس JSONParser می باشد که نحوه کار آن در کد نشان داده شده است.Itpro باشید

نویسنده: مهدی عادلی فر

منبع: انجمن تخصصی فناوری اطلاعات ایران

هرگونه نشر و کپی برداری بدون ذکر منبع و نام نویسنده دارای اشکال اخلاقی می باشد.


مهدی عادلی فر
مهدی عادلی فر

بنیانگذار توسینسو و برنامه نویس

مهدی عادلی، بنیان گذار TOSINSO. کارشناس ارشد نرم افزار کامپیوتر از دانشگاه صنعتی امیرکبیر و #C و جاوا و اندروید کار می کنم. در زمینه های موبایل و وب و ویندوز فعالیت دارم و به طراحی نرم افزار و اصول مهندسی نرم افزار علاقه مندم.

نظرات