當前位置:首頁 » 編程語言 » json解析java

json解析java

發布時間: 2022-06-11 21:31:28

java解析json文件

根據數據顯示,childern是個數組,那麼請問id是否跟此數組的下標相同,如果相同,那麼直接用

JSONArrayjsa=newJSONArray(data);//data為你獲取的json字元串
JSONObjectjObj=jsa.get(index)//index為下標
Stringvalue=jObj.getString("value");

如果不是下標,那麼只能遍歷整個數組,匹配你指定的id的json對象,然後從此對象中獲得value,獲取的方式同上

㈡ json解析,java該如何解析啊

一、 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;
}

㈢ 如何java解析json數組

工具/原料

  • 安裝有eclipse軟體的電腦一台

  • 方法/步驟

  • 1

    首先我們在eclipse中創建一個java工程,在java工程中創建一個HelloWorld的類,在這個java的入口程序中,我們來創建一個json字元串,並且列印出來,如下圖:

㈣ json數據如何用java解析啊

importjava.util.Iterator;

importnet.sf.json.JSONArray;
importnet.sf.json.JSONObject;

publicclassTest6
{
publicstaticvoidmain(String[]args)
{
Stringstr=
"{"info":{ "chuanyi":[ "舒適", "建議著長袖T恤、襯衫加單褲等服裝。年老體弱者宜著針織長袖襯衫、馬甲和長褲。" ], "ganmao":[ "易發", "晝夜溫差大,且空氣濕度較大,易發生感冒,請注意適當增減衣服,加強自我防護避免感冒。" ]}}";
JSONObjectjo=JSONObject.fromString(str);
JSONObjectinfo=jo.getJSONObject("info");
System.out.println(info);
Iteratorit=info.keys();
while(it.hasNext())
{
Stringkey=(String)it.next();
JSONArrayarray=info.getJSONArray(key);
for(inti=0;i<array.length();i++)
{
System.out.println(array.get(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");Iterator<JSONArray> itr = jsonArr.iterator();JSONArray temp;while(itr.hasNext()) {temp = itr.next();System.out.println("===========Each JSONArray=========");for(int i = 0; i<temp.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怎麼解析java

一、 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;
}

㈦ JSON 解析(java代碼)

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) {
String json = "你的json";
JSONArray array = JSONArray.fromObject(json);
JSONObject object = array.getJSONObject(0);
System.out.println(object.get("pm25"));
System.out.println(object.get("currentCity"));
System.out.println();
array = object.getJSONArray("");
for (Object obj : array) {
JSONObject o = JSONObject.fromObject(obj);
System.out.println(o.get("wind"));
System.out.println(o.get("weather"));
System.out.println(o.get("nightPictureUrl"));
System.out.println(o.get("date"));
System.out.println(o.get("dayPictureUrl"));
System.out.println(o.get("temperature"));
System.out.println();
}
array = object.getJSONArray("index");
for (Object obj : array) {
JSONObject o = JSONObject.fromObject(obj);
System.out.println(o.get("zs"));
System.out.println(o.get("des"));
System.out.println(o.get("tipt"));
System.out.println(o.get("title"));
System.out.println();
}
}
}

㈧ java解析json

JSON轉成java集合簡單步驟:1,載入:JSONArray jarr=JSONArray.fromObject(json字元串名);
2,轉換:List<類型> list=(List<類型>)JSONArray.toColleaction(jarr,類型.class);
JSON轉成java對象步驟,1,同上
2,類型如Emp(員工類)
Emp e=(Emp)JSONObject.toBean(jarr,Emp.class);

前提需要導入common的一系列的類庫,大概有6個jar包吧。
後面的需要你自己找你需要的某個字元串了。不知道是不是你所說的意思。

㈨ 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;
}

熱點內容
bin存儲 發布:2025-02-07 20:00:50 瀏覽:202
android載入界面 發布:2025-02-07 19:55:28 瀏覽:870
好礦雲伺服器 發布:2025-02-07 19:54:31 瀏覽:948
java電話簿 發布:2025-02-07 19:49:26 瀏覽:796
超級腳本製作 發布:2025-02-07 19:31:30 瀏覽:486
怎麼查看支付寶的賬號密碼 發布:2025-02-07 19:26:48 瀏覽:16
惠普伺服器查看ip指令 發布:2025-02-07 19:26:47 瀏覽:434
演算法設計模式 發布:2025-02-07 19:15:52 瀏覽:746
伺服器1u能連接幾台電腦 發布:2025-02-07 18:50:02 瀏覽:154
立人編譯 發布:2025-02-07 18:48:32 瀏覽:766