A. 解析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 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;
}
B. 如何生成和解析json格式數據
JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。它基於JavaScript的一個子集。
JSON採用完全獨立於語言的文本格式,但是也使用了類似於C語言家族的習慣(包括C, C++, C#, Java, JavaScript,
Perl, Python等)。這些特性使JSON成為理想的數據交換語言。易於人閱讀和編寫,同時也易於機器解析和生成,json生成的數據量比XML 還要少一些,所以很多公司傳輸數據喜歡用JSON數據格式 。
1、下面通過一個簡單的例子來說明JSON 的解析
String strJson = "{\"students\":[{\"name\":\"luci\",\"age\":23}, {\"name\":\"jack\",\"age\":25}, {\"name\":\"Kas\",\"age\":22}]}";
try {
JSONObject mBj = new JSONObject(strJson);//生成對象
JSONArray mJsonArray = (JSONArray) mBj.get("students");//取得數據數組
for (int i = 0; i < mJsonArray.length(); ++i) {
JSONObject mObject = (JSONObject) mJsonArray.get(i);
Log.d("log.d","name:" + mObject.getString("name") + "," + "age:"
+ mObject.getInt("age"));
}
} catch (JSONException e) {
e.printStackTrace();
}
json 數據解析小技巧,通過debug 方式查看對象數據格式。
2、json數據生成
try {
JSONObject mBj=new JSONObject();
JSONArray mJsonArray=new JSONArray();
mBj.put("students", mJsonArray);//存入主對象
JSONObject mJSONObject1=new JSONObject();
mJSONObject1.put("name", "luci");
mJSONObject1.put("age", 23);
mJsonArray.put(mJSONObject1);//存入數組對象
Log.d("log.d", mBj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
C. json格式怎麼打開
JSON格式的數據可以通過多種方式打開和解析,以下是一些常見的方法:
使用Python的json模塊:
文本編輯器:
在線JSON查看器:
專用軟體:
注意:無論使用哪種方法,確保JSON數據的結構規范性很重要,如鍵值對的形式、數據類型的明確以及可能的嵌套結構。這些規范性是成功打開和解析JSON內容的基礎。
D. Hive sql - 解析 JSON 格式數據
在Hive SQL中,JSON格式數據作為一種常用的數據交換格式,它以鍵值對的形式呈現,易於跨語言傳遞。其基本結構為{'Key': 'Value'},key用雙引號包圍,value可以是多種數據類型,如數值、字元串、布爾值等,甚至可以是數組或對象的嵌套結構。
解析JSON數據在Hive中主要涉及獲取特定鍵的值。對於單條JSON字元串,我們可以利用get_json_object()方法獲取指定鍵的值,如:
基礎語法如下:
而對於獲取多個鍵的值,json_tuple()方法只適用於獲取JSON的外層鍵值對,示例如下:
對於處理連續的字元串,Hive提供了explode()函數,可以根據需求進行操作。例如,如果要按行為單位逐條輸出,用explode(array(字元串));如果需要以Key-Value格式輸出,就使用explode(map(字元串))。
這些函數在處理JSON數據時,能夠幫助我們有效地提取所需信息,實現數據的高效處理和分析。