JSON Java – cov ntaub ntawv thiab cov nws xwb li cas?

JSON and Java

JSON thiab Java – Cas nws xwb?

Txheej txheem cej luam:

JSON, which is the abbreviation of JavaScript Object Notation, yog muaj ntaub ntawv kuas txauv hom ntawv uas siv rau cov kev siv lub web thiab teb neeg rau zaub mov. JSON happens to be a lot more compact and lighter when compared to XML. Besides, JSON objects are supported by numerous technologies and are also easy to write and read. Li ntawd, it is undoubtedly used by a majority of developers these days and is equally popular in web services with Java.

Nyob rau cov tshooj no, we will take a look at different sections of JSON and how it can be used in Java programming.







What are the characteristics and uses of JSON?

JSON has three primary characteristics that make it suitable for use with Java. These also have a lot to do with the popularity of the format in the present times. Cov no yog –

  • Reading and writing JSON is very easy.
  • JSON is lightweight and makes use of a text-based exchangeable format.
  • It is independent of other languages and supported by a majority of them.

Coming to the use of JSON, there are quite a few of them that we shall look into. Cov no yog –

  • JSON is largely used for writing applications that are based on JavaScript.
  • The format is preferred for the serialization and transmission of structured data over any network.
  • The main use of JSON is to transmit data between web applications and web servers.
  • APIs and web services are known to make use of JSON format in order to provide public data.
  • JSON is supported by modern technologies and can be used with modern programming languages.

Java APIs for JSON processing

JSR 353 is the Java API that is used for JSON processing. It is known to offer portable APIs for generation, parsing, transformation thiab querying ntawm JSON cov ntaub ntawv los ua siv cov qauv thiab streaming ntawm APIs.

Kwv Yees API – Cov Qauv Siv API yog zoo nkauj npaum li tus DOM Parser thiab lom zem ntau favourable rau cov khoom uas muaj me me. Cov qauv no yog paub tias tsim ib tug qauv treelike qauv rau cov ntaub ntawv JSON hauv nco uas yuav randomly accessed. Tsob ntoo zoo li qauv yuav queried thiab navigated. Cov qauv no mas nws yeej proves yuav yooj ywm thiab tso cai rau txoj kev uas ua rau nws tau randomly saib tag nrho cov ntsiab lus ntawm tsob ntoo. Txawm li cas los, Thaum nws piv rau cov qauv streaming qauv, Nws ntog luv xaiv tias nws yuav tsis npaum thiab kuj yuav tsum tau nco ntsoov nco.



Streaming API – Favourable rau cov teeb meem uas koj soj ntsuam nrog ib cim xeeb thiab tsis xav kom cov khoom tseem ceeb nyob rau hauv nco, Tus Streaming API zoo ib yam li cov StaX Parser. Nws muaj ib txoj kev uas tso cai rau cov parsing thiab tiam ntawm JSON hauv ib streaming manner. Cov API paub tias muaj ib parser uas yog kev tshwm sim raws li, Uas ntxiv tso cai rau tus tsim tawm ntawm daim ntawv thov kev tshwm sim tom ntej no tsis txhob coj mus kov cov kev tshwm sim nyob rau hauv ib callback. Qhov no yog ib yam uas tso cai rau cov coder kom muaj ntau txoj kev tswj txoj kev ntawm JSON cov ntaub ntawv. Nws pub rau daim ntawv thov kom muaj txheej txheem los yog pov tseg cov parser kev tshwm sim thiab ces nug rau yavtom ntej yavtom ntej. No streaming qauv no tshwj xeeb tshaj yog pab tau qhov twg random mus saib tau ntau qhov chaw hauv cov ntaub ntawv yuav tsis tas yuav tab sis theej, tej yam uas yuav tsum tau ua. Nyob rau hauv ib tug zoo sib xws, Tus streaming API no kuj zoo rau cov tiam zoo ua JSON cov ntaub ntawv nyob rau hauv ib kwj dej, sau ib qho kev tshwm sim thaum lub sij hawm.

Thib peb tog Java APIs rau JSON processing

Dhau li ntawm lub Java API, Kuj muaj ob peb tog Java libraries uas yuav siv tau rau tus txheej txheem ntawm JSON cov ntaub ntawv. Peb yuav tsum cia li saib peb xwb.

Jackson

Jackson Yog ib lub tsev qiv ntawv nrov heev raws li nyob rau hauv Java thiab siv rau lub serialization thiab mapping ntawm Java khoom rau JSON thiab lwm-versa. Jackson muaj kuj proven kom tau ib tug npaum API.

JSON.yooj yim

Thaum lub npe qhia, JSON.yooj yim Yog ib lub tsev qiv ntawv yooj yim heev rau nyeem ntawv thiab sau cov ntaub ntawv.

Google Gson

Google Gson Yog ib tug zoo nkauj nrov thib peb tog API uas siv rau pej xeem heev tej yaam num hnub no. Chiv, ho, Qhov no API yog tsim los ntawm Google siv nyob rau hauv lub koom haum.

Teeb ib puag ncig

Thov ua raws li cov kauj ruam hauv qab no teev cov kev pab npaj khiav cov kev pab cuam Java.

  • Down load load JDK ntawm qhov txuas thiab muab cov chav kawm ntawv

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Tam sim no koj cheeb tsam yog npaj khiav cov ntaub ntawv qauv.








Qauv Siv

Nyob rau hauv seem no peb yuav xyuas cov qauv sau thiab nyeem JSON cov ntaub ntawv uas siv Java programs. Tom qab yog peb tes num qauv tsim hauv Eclipse IDE.

Eclipse project structure

Image1: Peb tes num qauv hauv Eclipse

Thawj tug qauv qhia tias, Peb tau tsim ib daim ntawv json hauv lub zos lawv thiab sau cov ntaub ntawv rau nws.

Qhia 1: Sau Ntawv JSON cov ntaub ntawv nyob rau hauv ib daim ntawv

pob techalpine;

ntshuam java.io.FileWriter;

ntshuam java.io.IOException;

import org.json.yooj yim.JSONArray;

import org.json.yooj yim.JSONObject;

Pej xeem hoob kawm JsonWriteDemo {

pej xeem tsis muaj dabtsis loj zoo li qub(Txoj hlua[] args) {

//Npaj JSON khoom

JSONObject jsonobj = tshiab JSONObject();

jsonobj.muab(“lub npe”, “techalpine.com”);

jsonobj.muab(“kev pab”, “kawm”);

//Npaj cov lus teev tseg

JSONArray msglist = tshiab JSONArray();

msglist.ntxiv(“kev kawm”);

msglist.ntxiv(“kev kawm”);

msglist.ntxiv(“yees duab tutorial”);

jsonobj.muab(“lus”, msglist);

sim {

//Sau ntawv mus rau ib daim ntawv thov

FileWriter filewrtr = tshiab FileWriter(“jsonfile.json”);

sau ntawv mus rau lub xeev uas sau ntawv(jsonobj.toJSONString());

filewrtr.flush();

kaw();

} ntes (IOException ex) {

ex.printStackTrace();

}

System.out.Print(jsonobj);

}

}

Tam sim no khiav qhov kev pab cuam los hais kom prompt los yog Eclipse IDE. Cov zis yuav muab tso tawm raws li qhia hauv qab no.

JSON output

Duab 2: Uas qhia tso zis rau hauv ib daim ntawv

Qhov no yog tus piv txwv thib ob, Qhov twg peb tau noj tib yam json cov ntaub ntawv tsim nyob rau hauv cov thawj piv txwv. Cov ntaub ntawv JSON yog iterated thiab tso tawm kom pom raws li ib qho tso zis.








Qhia 2: Nyeem ntawv JSON cov ntaub ntawv los ntawm ib daim ntawv thov

pob techalpine;

ntshuam java.io.FileNotFoundException;

import java.io.FileReader;

ntshuam java.io.IOException;

import java.util.Iterator;

import org.json.yooj yim.JSONArray;

import org.json.yooj yim.JSONObject;

import org.json.yooj yim.parser.JSONParser;

Import org.json.yooj yim.parser.ParseException;

Pej xeem hoob kawm JsonReadDemo {

@SuppressWarnings(“tsis muaj zog”)

pej xeem tsis muaj dabtsis loj zoo li qub(Txoj hlua[] args) {

JSONParser jsnprsr = tshiab JSONParser();

sim {

//Nyeem los ntawm JSON ntaub ntawv

Object fileobj = jsnprsr.parse(Ntaub Ntawv Tshiab(“jsonfile.json”));

//Ua tau ib yam twj paj nruas JSON

JSONObject jsonObject = (JSONObject) foob;

String orgname = (Txoj hlua) jsonObject.get(“lub npe”);

System.out.println(“Koom haum lub npe: “+orgname);

Txoj hlua = (Txoj hlua)jsonObject.get(“kev pab”);

System.out.println(“Kev Pab Lub Npe: “+kev pab);

//Iterate los ntawm cov lus loop

JSONArray jsnmsg = (JSONArray) jsonObject.get(“lus”);

Iterator<Txoj hlua> jsniterate = jsnmsg.iterator();

System.out.println(“lus xov :- “);

Thaum (jsniterate.hasNext()) {

System.out.println(jsniterate.tom ntej no ntxiv());

}

} ntes (FileNotFoundException ex) {

ex.printStackTrace();

} ntes (IOException ex) {

ex.printStackTrace();

} ntes (ParseException ex) {

ex.printStackTrace();

}

}

}

Tam sim no khiav qhov kev pab cuam los hais kom prompt los yog Eclipse IDE. Cov zis yuav muab tso tawm raws li qhia hauv qab no.







JSON output

 

Duab 3: Uas qhia tso zis tshwm simXaus:

Ua hauj lwm nrog JSON cov ntaub ntawv hauv Java yuav haum yooj yim, Xaiv qhov HEEV APIs uas muaj rau cov hauj lwm ua hauj lwm. Siv cov APIs, ib tug neeg muaj mob tau ib tug –

  • Parse input streams rau immutable khoom/txheej xwm streams
  • Sau immutable khoom/event streams tso zis streams
  • Navigate immutable khoom siv cov kev pab cuam
  • Siv cov kev pab cuam los tsim cov khoom immutable.

Tsab xov xwm saum toj no qha tau tus txiv neej uas ib tug yuav ua hauj lwm nrog JSON cov ntaub ntawv nyob rau hauv Java. Yog xav xaiv qhov API, Ib tug yuav lo rau lub Java API rau JSON los txawm mus nrog tus neeg thib peb tog APIs uas muaj.


Tagged: ,
============================================= ============================================== Yuav zoo TechAlpine phau ntawv rau Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Txaus siab rau qhov blog? Tshaj tawm lus thov :)

Follow by Email
LinkedIn
LinkedIn
Share