1.pom.xml配置
com.googlecode.json-simple
json-simple
1.1
准备JSON数据文件jsonTestFile.json
{
' id': 1,
' firstname': 'Katerina ',
' languages': [
{
' lang': 'en ',
“知识3360”配置文件”
(,
{
' lang': 'fr ',
“知识”:“高级”
}
]、
' job': {
' site ' : ' www.Java code geeks.com ',
' name': 'Java Code Geeks '
}
}
3.Java分析类
package com.Java code geeks.Java basics.jsonparsertest;
import Java.io.file not found exception;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
importorg.JSON.simple.JSON array;
importorg.JSON.simple.JSON object;
importorg.JSON.simple.parser.JSON parser;
importorg.JSON.simple.parser.parse exception;
公共类jsonparsetest {
privatestaticfinalstringfilepath=' c :\ usersKaterinadesktop\ JSON test file.JSON ';
publicstaticvoidmain(string仁爱太阳args )。
try {
//读我的JSON文件
jsonparserjsonparser=newjsonparser (;
//get a String from the JSON object
String firstName=(String ) JSONobject.get('firstname );
//get a number from the JSON object
longid=(long ) JSONobject.get ) ) id );
//get an array from the JSON object
//taketheelementsofthejsonarray
for(intI=0; I
}
Iterator i=lang.iterator (;
//takeeachvaluefromthejsonarrayseparately
wile(I.Hasnext ) ) ) )。
' with level ' inner obj.get (' knowledge );
}
//handleastructureintothejsonobject
}catch(filenotfoundexceptionex ) {
ex.printStackTrace (;
}catch(ioexceptionex ) {
ex.printStackTrace (;
}catch(parseexceptionex ) {
ex.printStackTrace (;
} catch (空pointerexceptionex ) {
ex.printStackTrace (;
}
}
}