java解析jsonnode,Java解析JSON文件

本文目錄一覽:

(java)解析json文件然後存入數據庫,幫一下忙啊,大神

你定義一個對應屬性的的vo類,對應數據庫表的字段。然後用json解析工具解析,屬性存到對應的VO類里。

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官網下載:)

然後將數據轉為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 ListPerson getPersons(String jsonStr)

{

ListPerson list = new ArrayListPerson();

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;

}

java中Json怎樣解析數據?

你這個JSON格式,就是數組裏面放數組,所以是,取JSON對象》取JSON數組data》取JSON數組。

import java.util.ArrayList;import java.util.Iterator;import net.sf.json.*;public class MainClass {/*** @param args*/public static void main(String[] args) {JSONObject jsonObj = JSONObject.fromObject(JsonData.getData());JSONArray jsonArr = jsonObj.getJSONArray(“data”);IteratorJSONArray itr = jsonArr.iterator();JSONArray temp;while(itr.hasNext()) {temp = itr.next();System.out.println(“===========Each JSONArray=========”);for(int i = 0; itemp.size(); i++) {System.out.println(temp.get(i));}}}private static class JsonData {private static String getData() {return “{\”data\”:[[5000235,2,3441,8,17,\”北京測試\”,\”10000101111\”,\”\”,\”\”,\”100001\”,\”\”,\”2011-09-23 17:20:07\”,18,\”vhcDefaultPwd\”,1,0,\”2011-09-20 00:00:00\”,12,0,380,\”測試\”,213,1,0,0,0,0,0,\”2012-11-05 14:35:23\”,\”\”],[5000236,27,3442,10,17,\”北京測試2\”,\”1230000\”,\”\”,\”\”,\”2010920002\”,\”111111\”,\”2011-09-23 17:20:08\”,18,\”vhcDefaultPwd\”,1,0,\”2011-09-20 00:00:00\”,12,0,380,\”測試2\”,213,1,0,0,0,0,0,\”2012-11-05 14:35:23\”,\”\”]]}”;}}}

java如何給有層級關係的JSON字符串做遞歸處理

先遞歸出一個 node對象,再將node對象轉換成json串。直接進行字符串的json操作不建議。

class Node{

private String id;

private String parentId;

private ListNode children;

public void addChildren(Node child){

getChildren().add(child);

}

public ListNode getChildren();

public String transformTojson(Node node ,StringBuffer jsonStr){

//遞歸

if(jsonStr ==null){

StringBuffer jsonStr=new ();

}

if(node.id!=null){

//拼接父節點json串

jsonStr.append(id:node.id,children:#children);

}

List childNodeList = node.getChildren();

StringBuffer childrenSB = new();

for( Node node :childNodeList){

childrenSB=transformTojson(node);

}

sonStr.replaceFirst(“#children”,childrenSB);

return jsonStr;

}

}

偽代碼大概這樣。

transformTojson方法可以不自己寫,直接使用第三方工具jar包的json轉化方法,比如gjson。

java 解析json字符串

你好:

後台拆分json

private String interactPrizeAll;    //json 使用字符串來接收

方法中的代碼:

Gson gson = new Gson();

InteractPrize interactPrize =new InteractPrize();

 //gson用泛型 轉List數組 多個對象

ListInteractPrize  interactPrizeList = gson.fromJson(interactPrizeAll, new TypeTokenListInteractPrize(){}.getType()); //TypeToken,它是gson提供的數據類型轉換器,可以支持各種數據集合類型轉換 

for(int i = 0; i  interactPrizeList.size(); i++)  

   {  

    interactPrize = interactPrizeList.get(i);//獲取每一個對象

    }

 這一種方法是轉單個對象時使用的

 //gson 轉對象 單個對象

//interactPrize = gson.fromJso(interactPrizeAll,InteractPrize.class);

這個方法是我後台拼的json往前台傳的方法

jsonStrAll.append(“{“+ “\””+”catid”+”\””+”:”+”\””+c.getCatid()+”\””+”,”+”\””+”catname”+”\””+”:”+”\””+c.getCatname()+”\””+”,”+ “\””+”catdesc”+”\””+”:”+”\””+c.getCatdesc()+”\””+”,”+”\””+”showinnav”+”\””+”:”+”\””+c.getShowinnav()+”\””+”,”+”\””+”sortorder”+”\””+”:”+”\””+c.getSortorder()+”\””+”,”+”level:”+”\””+”0″+”\””+”,parent:”+”\””+”0″+”\””+”,isLeaf:true,expanded:false,”+”loaded:true},”);

你自己挑着用吧!

java怎麼處理json格式數據

 1、通過谷歌的Gson來進行解析: 

json數據:sTotalString = {“message”:”success”,”result”:[{“surveyid”:”1″,”surveyname”:”B”}{surveyid”:”2″,”surveyname”:”C”}]};

2、通過json-org.jar包進行解析: 

json數據:sTotalString = {“message”:”success”,”result”:[{“surveyid”:”1″,”surveyname”:”B”}{surveyid”:”2″,”surveyname”:”C”}]};

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/284961.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-22 15:42
下一篇 2024-12-22 15:43

相關推薦

發表回復

登錄後才能評論