本文目錄一覽:
- 1、java實體類怎麼轉換成json。
- 2、java中 如何 根據Class 對象,將Object轉換成對應的類型
- 3、Grails中把實體類轉換為JSONObject對象時報錯:There is a cycle in the hierarchy!
- 4、class類型的json怎麼解析
- 5、怎樣將一個類轉換為json對象
java實體類怎麼轉換成json。
導入Google的包gson-2.2.4.jar
然後實例化Gson
static Gson gosn = new Gson();
String json = gosn.toJson(hashMap); //這裡放一個對象,什麼對象都可以。
轉化後就是Json,功能強大很多,也簡單很多。
json-lib-2.4-jdk15.jar
ezmorph-1.0.6.jar
轉換的話這樣用
String s= JSONArray.fromObject(user).toString();
spring-webmvc4
在方法上加入@ResponseBody,同時方法返回值為實體對象,spring會自動將對象轉換為json格式,並返回到客戶端
java中 如何 根據Class 對象,將Object轉換成對應的類型
GoodsListGetResponseVO goodsListGetResponseVO = JSONObject.parseObject(jsonObject.get(“goodsListGetResponse”).toString(), GoodsListGetResponseVO.class);
把一個object轉化成你自定義類對象,你首先得確定你的object是那種類型?
不然強行轉肯定會出錯,就算是把json object轉自定義class 也要字段能對應上才能轉,需要你自定義個轉化得方法。
如果是json轉自定義類,可以用阿里得fastJson包,上面一行代碼就是示例
Grails中把實體類轉換為JSONObject對象時報錯:There is a cycle in the hierarchy!
應該是你的對象包含了對自身的引用,轉換json時進行遞歸出現這個錯誤。
class類型的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;
}
怎樣將一個類轉換為json對象
給你個最簡單的例子:引用Newtonsoft.Json.dll
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Name = “guwei4037”;
student.Age = 7;
string json = JsonConvert.SerializeObject(student);//序列化輸出Json字符串
Console.WriteLine(json);
Student jsonObject = JsonConvert.DeserializeObjectStudent(json);
Console.WriteLine(string.Join(” “, jsonObject.Name, jsonObject.Age));//反序列化輸出 guwei4037和7
}
}
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
原創文章,作者:TK05M,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/130539.html