本文目錄一覽:
- 1、java 如何解析JSON
- 2、java中Json怎樣解析數據?
- 3、如何將json的數據轉化成csv的數據格式
- 4、如何用原生JS來把JSON數據處理成CSV格式
- 5、java 讀取文件並解析json格式數據再存入數據庫
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官網下載:)然後將數據轉為json字符串,核心函數是:publicstaticStringcreateJsonString(Stringkey,Objectvalue){JSONObjectjsonObject=newJSONObject();jsonObject.put(key,value);returnjsonObject.toString();}B、客戶端將json字符串轉換為相應的javaBean1、客戶端獲取json字符串(因為android項目中已經集成了json的jar包所以這裡無需導入)publicclassHttpUtil{publicstaticStringgetJsonContent(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””;}privatestaticStringConvertStream2Json(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、獲取javaBeanpublicstaticPersongetPerson(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
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\”,\”\”]]}”;}}}
如何將json的數據轉化成csv的數據格式
請問你是在什麼語言中?是js對象弄到java對象還是java發過來在頁面中看到的?還是其他語言c#php?如果是js到java可以用google的gson很簡單如果是java發的js給頁面直接循環就可以拿到了
如何用原生JS來把JSON數據處理成CSV格式
json轉csv的前提是,你的數據是jsonarray,而且只有比較特殊的數據可以在json和csv之間轉換,請轉換之前判斷數據是否可以轉換。
思路是,遍歷array里所有數據,將每條數據設置成一個jsonobject,如果拋出一場則條過這條數據continue到下一條。然後遍歷jsonobject的所有屬性,將每個屬性的值用逗號分割拼成字符串,所有屬性遍歷完畢後,判斷如果當前這條不是array的最後一條,則在字符串最後加上一個’\r’,否則不加。最終會拼成csv字符串。
java 讀取文件並解析json格式數據再存入數據庫
寫一個java類,包含這些屬性,然後用fastjson的包可以直接轉換成這個類的對象,還有可以把後綴改成.json可以用fastjson里的方法直接解析
原創文章,作者:ASXNP,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/130712.html