首页 > 编程知识 正文

json的方法,java解析json字符串

时间:2023-05-05 07:01:10 阅读:110391 作者:3015

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

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。