用JSONObject這個類庫
用谷歌給我們提供的Gson
使用方式:
JSONObject
json=
new
JSONObject(json);
json.getString(key);
Object
obj
=
new
Gson().fromJson(json,Object.class);
將這個object替換成你的javaBean
② JSONObject獲取值後為一個對象,將對象轉為JSONObject時報錯
將json字元串轉化成JavaBean對象
Person person = new Person("1","gson",1);
Gson gson = new Gson();
//這里將javabean轉化成json字元串
String jsonString = gson.toJson(person);
System.out.println(jsonString);
//這里將json字元串轉化成javabean對象,
person = gson.fromJson(jsonString,Person.class);
將json字元串轉化成List<JavaBean>對象
Person person1 = new Person("1","gson1",1);
Person person2 = new Person("2","gson2",2);
List<Person> persons = new ArrayList<Person>();
persons.add(person1);
persons.add(person2);
Gson gson = new Gson();
//這里將lsit<javabean>轉化成json字元串
String jsonString = gson.toJson(persons);
//解析json字元串
List<Person> persons2 = gson.fromJson(jsonString, new TypeToken<List<Person>>(){}.getType());
將json字元串轉化成List<String>對象
List<String> list = new ArrayList<String>();
list.add("gson1");
list.add("gson2");
list.add("gson3");
Gson gson = new Gson();
String jsonString = gson.toJson(list);
System.out.println("json字元串:"+jsonString);
//解析json字元串
List<String> list2 = gson.fromJson(jsonString, new TypeToken<List<String>>(){}.getType());
將json字元串轉化成List<Map<String,Object>>對象
Map<String,Object> map = new HashMap<String,Object>();
map.put("key1", "value1");
map.put("key2", "value2");
Map<String,Object> map2 = new HashMap<String,Object>();
map2.put("key1", 1);
map2.put("key2", 2);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
list.add(map);
list.add(map2);
Gson gson = new Gson();
String jsonString = gson.toJson(list);
System.out.println("json字元串:"+jsonString);
//解析json字元串
List<Map<String,Object>> list2 = gson.fromJson(jsonString, new TypeToken<List<Map<String,Object>>>(){}.getType());
③ 如何將json格式轉換為數組
java中把來json怎麼轉換成數組的方法:
1、使用自原生的解析:String json = "...";JSONArray array= new JSONArray(json);//遍歷數組里的值,得到每個獨立的對象,然後獲取對應的值設置到聲明好的對象中,最終創建對象完成後添加到集合中,如我自己代碼里的片段:for (int j = 0; j < array.length(); j++) {obj = array.getJSONObject(j);Data data = new Data();data.setThumbnail(obj.getString("thumbnail"));data.setTitle(obj.getString("title"));data.setUrl(obj.getString("url"));mDataList.add(data);}2、使用第三方包如Gson,但是這個你得保證你的JSON字元串個z
④ list<model>類型轉換為json格式的,在線等大神的幫忙
public class TestJson {
public static void main(String[] args) {
JavaBeanAndJson();
// ListJavaBeanAndJson();
// JsonAndString();
// JsonAndListString();
// JsonAndListMapString();
}
/**
* 將json字元串轉化成<Map<String,Object>>對象
*/
private static void JsonAndListMapString() {
System.out.println("將json字元串轉化成List<Map<String,Object>>對象");
Map<String,Object> map = new HashMap<String,Object>();
map.put("key1", "value1");
map.put("key2", "value2");
Map<String,Object> map2 = new HashMap<String,Object>();
map2.put("key1", 1);
map2.put("key2", 2);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
list.add(map);
list.add(map2);
String jsonString = JSON.toJSONString(list);
System.out.println("json字元串:"+jsonString);
// 解析json字元串
List<Map<String,Object>> list2 = JSON.parseObject(jsonString, new TypeToken<List<Map<String,Object>>>(){}.getType());
}
/**
* 將json字元串轉化成List<String>對象
*/
private static void JsonAndListString() {
System.out.println("將json字元串轉化成List<String>對象");
List<String> list = new ArrayList<String>();
list.add("gson1");
list.add("gson2");
list.add("gson3");
String jsonString = JSON.toJSONString(list);
System.out.println("json字元串:"+jsonString);
// 解析json字元串
List<String> list2 = JSON.parseObject(jsonString, new TypeToken<List<String>>(){}.getType());
}
/**
* 將json字元串轉化成List<String>對象
*/
private static void JsonAndString() {
System.out.println("將json字元串轉化成List<String>對象");
List<String> list = new ArrayList<String>();
list.add("gson1");
list.add("gson2");
list.add("gson3");
String jsonString = JSON.toJSONString(list);
System.out.println("json字元串:"+jsonString);
// 解析json字元串
List<String> list2 = JSON.parseObject(jsonString, new TypeToken<List<String>>(){}.getType());
}
/**
* 將json字元串轉化成List<JavaBean>對象
*/
private static void ListJavaBeanAndJson() {
System.out.println("將json字元串轉化成List<JavaBean>對象");
Person person1 = new Person("1","gson1",1);
Person person2 = new Person("2","gson2",2);
List<Person> lstPersons = new ArrayList<Person>();
lstPersons.add(person1);
lstPersons.add(person2);
// 這里將lsit<javabean>轉化成json字元串
String jsonString = JSON.toJSONString(lstPersons);
System.out.println(jsonString);
// 解析json字元串
List<Person> lstPersons2 = JSON.parseObject(jsonString, new TypeToken<List<Person>>(){}.getType());
}
/**
* 將json字元串轉化成JavaBean對象
*/
private static void JavaBeanAndJson() {
System.out.println("將json字元串轉化成JavaBean對象");
Person person = new Person("1","fastjson",1);
// 這里將javabean轉化成json字元串
String jsonString = JSON.toJSONString(person);
System.out.println(jsonString);
// 這里將json字元串轉化成javabean對象,
Person person2 = JSON.parseObject(jsonString,Person.class);
System.out.println(person2);
}
}