导航:首页 > 数据处理 > java如何解析json数据

java如何解析json数据

发布时间:2022-01-23 02:48:55

1. 用java解析json 格式的字符.该如何解析

String temp="{'data':{'a':[{'b1':'bb1','c1':'cc1'},{'b2':'bb2','c2':'cc2'}]}}";
JSONObject jodata =JSONObject.fromObject(temp);
JSONObject joa =JSONObject.fromObject(jodata.get("data").toString());
JSONArray ja=JSONArray.fromObject(joa.get("a"));
for(int i=0;i<ja.size();i++){
JSONObject o=ja.getJSONObject(i);
if(o.get("b1")!=null){
System.out.println(o.get("b1"));
}
if(o.get("c1")!=null){
System.out.println(o.get("c1"));
}
if(o.get("b2")!=null){
System.out.println(o.get("b2"));
}
if(o.get("c2")!=null){
System.out.println(o.get("c2"));
}
}
}
注:要包含两个jar包ezmorph-1.0.6.jar和json-lib-2.2.2-jdk15.jar,jar包在附件中

2. java怎么解析我这个json数据

你这个JSON格式,就是数组里面放数组,所以是,取JSON对象》取JSON数组data》取JSON数组。

importjava.util.ArrayList;
importjava.util.Iterator;
importnet.sf.json.*;
publicclassMainClass{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
JSONObjectjsonObj=JSONObject.fromObject(JsonData.getData());
JSONArrayjsonArr=jsonObj.getJSONArray("data");
Iterator<JSONArray>itr=jsonArr.iterator();
JSONArraytemp;
while(itr.hasNext()){
temp=itr.next();
System.out.println("===========EachJSONArray=========");
for(inti=0;i<temp.size();i++){
System.out.println(temp.get(i));
}
}
}
privatestaticclassJsonData{
privatestaticStringgetData(){
return"{"data":[[5000235,2,3441,8,17,"北京测试","10000101111","","","100001","","2011-09-2317:20:07",18,"vhcDefaultPwd",1,0,"2011-09-2000:00:00",12,0,380,"测试",213,1,0,0,0,0,0,"2012-11-0514:35:23",""],[5000236,27,3442,10,17,"北京测试2","1230000","","","2010920002","111111","2011-09-2317:20:08",18,"vhcDefaultPwd",1,0,"2011-09-2000:00:00",12,0,380,"测试2",213,1,0,0,0,0,0,"2012-11-0514:35:23",""]]}";
}
}
}

3. java 如何解析JSON

一、JSON(JavaScriptObjectNotation)一种简单的数据格式,比xml更轻巧。Json建构于两种结构:1、“名称/值”对的集合(Acollectionofname/valuepairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hashtable),有键列表(keyedlist),或者关联数组(associativearray)。如:{“name”:”jackson”,“age”:100}2、值的有序列表(Anorderedlistofvalues)。在大部分语言中,它被理解为数组(array)如:{“students”:[{“name”:”jackson”,“age”:100},{“name”:”michael”,”age”:51}]}二、java解析JSON步骤A、服务器端将数据转换成json字符串首先、服务器端项目要导入json的jar包和json所依赖的jar包至builtPath路径下(这些可以到JSON-lib官网下载:http://json-lib.sourceforge.net/)然后将数据转为json字符串,核心函数是:(Stringkey,Objectvalue){JSONObjectjsonObject=newJSONObject();jsonObject.put(key,value);returnjsonObject.toString();}B、客户端将json字符串转换为相应的javaBean1、客户端获取json字符串(因为android项目中已经集成了json的jar包所以这里无需导入)publicclassHttpUtil{(StringurlStr){try{//获取HttpURLConnection连接对象URLurl=newURL(urlStr);HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();//设置连接属性httpConn.setConnectTimeout(3000);httpConn.setDoInput(true);httpConn.setRequestMethod("GET");//获取相应码intrespCode=httpConn.getResponseCode();if(respCode==200){returnConvertStream2Json(httpConn.getInputStream());}}catch(MalformedURLExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}return"";}(InputStreaminputStream){StringjsonStr="";//ByteArrayOutputStream相当于内存输出流ByteArrayOutputStreamout=newByteArrayOutputStream();byte[]buffer=newbyte[1024];intlen=0;//将输入流转移到内存输出流中try{while((len=inputStream.read(buffer,0,buffer.length))!=-1){out.write(buffer,0,len);}//将内存流转换为字符串jsonStr=newString(out.toByteArray());}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnjsonStr;}}2、获取(StringjsonStr){Personperson=newPerson();try{//将json字符串转换为json对象JSONObjectjsonObj=newJSONObject(jsonStr);//得到指定jsonkey对象的value对象JSONObjectpersonObj=jsonObj.getJSONObject("person");//获取之对象的所有属性person.setId(personObj.getInt("id"));person.setName(personObj.getString("name"));person.setAddress(personObj.getString("address"));}catch(JSONExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}returnperson;}publicstaticListgetPersons(StringjsonStr){Listlist=newArrayList();JSONObjectjsonObj;try{//将json字符串转换为json对象jsonObj=newJSONObject(jsonStr);//得到指定jsonkey对象的value对象JSONArraypersonList=jsonObj.getJSONArray("persons");//遍历jsonArrayfor(inti=0;i

4. java获取json数据方法

你这就是一个Extjs grid 的JsonStore

放到JAVA里的话要先转成对象

importnet.sf.json.JSONObject;

publicclassTestJson{
staticStringjson_str="{"total":920,"data":[{"ID":"634","Name":"于东"},{"ID":"822","Name":"于祎"},{"ID":"782","Name":"于燕"},{"ID":"636","Name":"于玲"},{"ID":"841","Name":"于浩"},{"ID":"383","Name":"于娟"}]}";
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
JSONObjectjsonObject=JSONObject.fromObject(json_str);
System.out.println(jsonObject.get("total"));
}

}

5. Java解析json数据

一、 JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧。
Json建构于两种结构:
1、“名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。 如:
{
“name”:”jackson”,
“age”:100
}

2、值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)如:
{
“students”:
[
{“name”:”jackson”,“age”:100},
{“name”:”michael”,”age”:51}
]
}
二、java解析JSON步骤
A、服务器端将数据转换成json字符串
首先、服务器端项目要导入json的jar包和json所依赖的jar包至builtPath路径下(这些可以到JSON-lib官网下载:http://json-lib.sourceforge.net/)

然后将数据转为json字符串,核心函数是:
public static String createJsonString(String key, Object value)
{
JSONObject jsonObject = new JSONObject();
jsonObject.put(key, value);
return jsonObject.toString();
}
B、客户端将json字符串转换为相应的javaBean
1、客户端获取json字符串(因为android项目中已经集成了json的jar包所以这里无需导入)
public class HttpUtil
{

public static String getJsonContent(String urlStr)
{
try
{// 获取HttpURLConnection连接对象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
// 设置连接属性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 获取相应码
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流转移到内存输出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 将内存流转换为字符串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
2、获取javaBean
public static Person getPerson(String jsonStr)
{
Person person = new Person();
try
{// 将json字符串转换为json对象
JSONObject jsonObj = new JSONObject(jsonStr);
// 得到指定json key对象的value对象
JSONObject personObj = jsonObj.getJSONObject("person");
// 获取之对象的所有属性
person.setId(personObj.getInt("id"));
person.setName(personObj.getString("name"));
person.setAddress(personObj.getString("address"));
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return person;
}

public static List<Person> getPersons(String jsonStr)
{
List<Person> list = new ArrayList<Person>();

JSONObject jsonObj;
try
{// 将json字符串转换为json对象
jsonObj = new JSONObject(jsonStr);
// 得到指定json key对象的value对象
JSONArray personList = jsonObj.getJSONArray("persons");
// 遍历jsonArray
for (int i = 0; i < personList.length(); i++)
{
// 获取每一个json对象
JSONObject jsonItem = personList.getJSONObject(i);
// 获取每一个json对象的值
Person person = new Person();
person.setId(jsonItem.getInt("id"));
person.setName(jsonItem.getString("name"));
person.setAddress(jsonItem.getString("address"));
list.add(person);
}
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

6. Java中如何解析Json的字符串得到result、serialNumber的值

import net.sf.json.JSONObject;

public class Test {
public static void main(String[] args) {
String json = "你的json";
JSONObject object = JSONObject.fromObject(json);
JSONObject alipay = object
.getJSONObject("alipay_pass_sync_add_response");
JSONObject biz = alipay.getJSONObject("biz_result");
System.out.println(biz.get("result"));
System.out.println(biz.get("serialNumber"));
System.out.println(alipay.get("error_code"));
System.out.println(alipay.get("success"));
}
}

7. java解析json数据

final String jsonData = "{\"success\":true}";
JSONObject jsonObj = JSONObject.fromObject(jsonData);
boolean success = jsonObj.getBoolean("success");

8. 请大神帮助,java如何解析json数据

java首先导入以下一个包
json-lib-2.3-jdk15.jar
commons-beanutils-1.7.0.jar
commons-httpclient-3.1.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-collections-3.1.jar
ezmorph-1.0.3

String dataStr = "{\"resultcode\":\"200\",.......}";
JSONObject json = JSONObject.fromObject(dataStr );
String resultcode = json.get('resultcode');

就是这样获取的;

String result = json.get('resultcode');
JSONObject resultJson = JSONObject.fromObject(result );
嵌套的json必须在重新解析

去了解下 JSON的相关api吧

9. java解析json数据成数组

importnet.sf.json.JSONArray;


publicclassTestJson
{
publicstaticvoidmain(String[]args)
{
Stringjson="[{"a":"111","b":"222","c":"333"},{"a":"1000","b":"2000","c":"000"},{"a":"999","b":"300","c":"700"}]";
JSONArrayjsonArr=JSONArray.fromObject(json);
Stringa[]=newString[jsonArr.size()];
Stringb[]=newString[jsonArr.size()];
Stringc[]=newString[jsonArr.size()];
for(inti=0;i<jsonArr.size();i++){
a[i]=jsonArr.getJSONObject(i).getString("a");
b[i]=jsonArr.getJSONObject(i).getString("b");
c[i]=jsonArr.getJSONObject(i).getString("c");
}

for(inti=0;i<c.length;i++){
System.out.print(a[i]+"");
System.out.print(b[i]+"");
System.out.print(c[i]);
System.out.println();
}
}
}

10. java如何读取json中文件内容

java读取文件非常简单的
Filefile=newFile("D:/java"); //给定一个目录
File[]list=file.listFiles(); //获取目录下的所有文件
for(inti=0;i<list.length;i++){
if(list[i].isFile()){ //判断是否为文件
InputStreamReaderisr=newInputStreamReader(newFileInputStream(list[i]),"UTF-8");//读取文件,同时指定编码
StringBuffersb=newStringBuffer();
char[]ch=newchar[128]; //一次读取128个字符
intlen=0;
while((len=isr.read(ch,0,ch.length))!=-1){
sb.append(ch,0,len);
}
isr.close();
System.out.println(sb); //将读取完的文件打印出来,你要怎么处理都可以了
}
}

阅读全文

与java如何解析json数据相关的资料

热点内容
北斗信息卡怎么更改信息 浏览:113
畅易阁小号什么时候可以交易啊 浏览:245
ttsservice是什么程序 浏览:647
怎么注册货运信息部 浏览:472
兼职天天快递代理点怎么样 浏览:654
成都千盛百货打折信息有哪些 浏览:269
程序bzc是什么 浏览:839
信息技术考了三次没考过怎么办 浏览:124
填充数据为什么按ctrl 浏览:67
什么样的部门能做市场调研 浏览:425
苹果固态硬盘数据怎么读取 浏览:687
已经更新的程序如何能返回旧版本 浏览:701
怎么在程序里查找零点 浏览:807
家居产品设计与展示是以前的什么专业 浏览:261
钉钉上为什么会有快递信息 浏览:511
finn是什么数据类型 浏览:718
王者荣耀交易猫怎么玩 浏览:624
创造营3数据统计的网页是什么 浏览:497
亚马逊虚拟产品怎么推广 浏览:296
如何进入研发级程序员 浏览:292