導航:首頁 > 編程語言 > c將json數組轉化成對象

c將json數組轉化成對象

發布時間:2024-05-02 14:13:59

A. 使用jackson怎樣把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

B. 用C#語言將json格式數據轉成json對象

把你的json字元串中的所有的 " 替換為 ""


比如我要在c#中聲明一個字元串

{
"a":"1"
}

寫法有兩種

1

stringjson="{
"a":"1"
}」;//因為內字元串需要用""來定義容所以其中的"要用"來轉義,而換行則是

2

stringjson=@"{
""a"":""1""
}";
//使用@方式定義字元串,這種方式關閉了x這種轉義序列,所以其中的"要用另外一個"來轉義於是就變成了""

C. 如何把這樣的json串轉換成對象

1.簡單的解析json字元串
首先將json字元串轉換為json對象,然後再解析json對象,過程如下。
JSONObject jsonObject = JSONObject.fromObject(jsonStr);

[java] view plain
<pre></pre><span style="white-space:pre"></span>
<pre></pre>
根據json中的鍵得到它的值
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
String sex = jsonObject.getString("sex");
int age = jsonObject.getInt("age");

2.將json字元串轉換為java對象
同樣先將json字元串轉換為json對象,再將json對象轉換為java對象,如下所示。
JSONObject obj = new JSONObject().fromObject(jsonStr);//將json字元串轉換為json對象

將json對象轉換為java對象
Person jb = (Person)JSONObject.toBean(obj,Person.class);//將建json對象轉換為Person對象

3.將java對象轉換為json字元串
先將java對象轉換為json對象,在將json對象轉換為json字元串
JSONObject json = JSONObject.fromObject(obj);//將java對象轉換為json對象

String str = json.toString();//將json對象轉換為字元串
完整代碼如下:

[java] view plain
package baz.parse;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import baz.bean.Person;

public class ParseJson {

private String jsonStr;

public ParseJson() {

}

public ParseJson(String str){
this.jsonStr = str;
}
/**
* 解析json字元串
*/
public void parse(){
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
String sex = jsonObject.getString("sex");
int age = jsonObject.getInt("age");

System.out.println(name + " " + num + " " + sex + " " + age);
}
//將json字元串轉換為java對象
public Person JSON2Object(){
//接收{}對象,此處接收數組對象會有異常
if(jsonStr.indexOf("[") != -1){
jsonStr = jsonStr.replace("[", "");
}
if(jsonStr.indexOf("]") != -1){
jsonStr = jsonStr.replace("]", "");
}
JSONObject obj = new JSONObject().fromObject(jsonStr);//將json字元串轉換為json對象
Person jb = (Person)JSONObject.toBean(obj,Person.class);//將建json對象轉換為Person對象
return jb;//返回一個Person對象
}

}

[java] view plain
package baz.bean;

public class Person {

private String name;
private int num;
private String sex;
private int age;

public Person() {
// TODO Auto-generated constructor stub
}

public Person(String name, int num, String sex, int age) {
super();
this.name = name;
this.num = num;
this.sex = sex;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}

將java對象轉換為json字元串

[java] view plain
package baz.cons;

import net.sf.json.JSONObject;

/**
* 將java對象轉換為json字元串
* @author Administrator
*
*/
public class ConsJson {

public ConsJson() {
// TODO Auto-generated constructor stub
}

public String Object2Json(Object obj){
JSONObject json = JSONObject.fromObject(obj);//將java對象轉換為json對象
String str = json.toString();//將json對象轉換為字元串

return str;
}
}

測試類:
[java] view plain
package baz.test;

import java.util.List;

import baz.bean.Person;
import baz.cons.ConsJson;
import baz.parse.ParseJson;

public class Test {
public static void main(String[] args) {

//將字元串轉換為json對象,然後根據建得到相應的值
ParseJson pj = new ParseJson("{\"name\":\"gu\",\"num\":123456,\"sex\":\"male\",\"age\":24}");
pj.parse();

//將一個json字元串轉換為java對象
Person p = pj.JSON2Object();
System.out.println("Name:" + p.getName());
System.out.println("Num:" + p.getNum());
System.out.println("Sex:" + p.getSex());
System.out.println("age:" + p.getAge());

//將一個java對象轉換為Json字元串
Person p1 = new Person("gu1",123,"male",23);
ConsJson cj = new ConsJson();
String str1 = cj.Object2Json(p1);
System.out.println(str1);

}

}
測試輸出如下:
gu 123456 male 24
Name:gu
Num:123456
Sex:male
age:24
{"age":23,"name":"gu1","num":123,"sex":"male"}

D. 怎麼把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

E. 如何將C#/.NET 將json字元串格式數據轉換成對象

下個Newtonsoft.Json插件
引用 Newtonsoft.Json.dll

1、json字元串
string xxx = "{\"count\":\"1\",\"Proct_Code\":\"14003949\",\"Proct_Name\":\"聚丙烯樹脂\",\"Proct_Batch\":\"20140228D8103\",\"Certification_Code\":\"SCSH20140226-001-01\",\"Plate_Code\":\"L5E-89\",\"Grade\":\"合格品\",\"WarehouseIn_Num\":\"19120.0000000\",\"WarehouseIn_Weight\":\"478.000\",\"WarehouseIn_Confirm_Date\":\"2014-03-01\"}";

則直接轉換為對象:

M_WarehouseInResult whh = JsonConvert.DeserializeObject<M_WarehouseInResult>(xxx);
2、如果為json數組(注意:最外是中括弧)
string xxx = "[{\"count\":\"1\",\"Proct_Code\":\"14003949\",\"Proct_Name\":\"聚丙烯樹脂\",\"Proct_Batch\":\"20140228D8103\",\"Certification_Code\":\"SCSH20140226-001-01\",\"Plate_Code\":\"L5E-89\",\"Grade\":\"合格品\",\"WarehouseIn_Num\":\"19120.0000000\",\"WarehouseIn_Weight\":\"478.000\",\"WarehouseIn_Confirm_Date\":\"2014-03-01\"}]";

json數組轉換為list

List<M_WarehouseInResult> whh = JsonConvert.DeserializeObject<List<M_WarehouseInResult>>(xxx);

附:
public class M_WarehouseInResult
{
public string count { get; set; }
public string Proct_Code { get; set; }
public string Proct_Name { get; set; }
public string Proct_Batch { get; set; }
public string Certification_Code { get; set; }
public string Plate_Code { get; set; }
public string Grade { get; set; }
public string WarehouseIn_Num { get; set; }
public string WarehouseIn_Weight { get; set; }
public string WarehouseIn_Confirm_Date { get; set; }
}

F. json數組轉java對象怎麼轉

首先需要commons-beanutils jar包,然後轉bean的方法為:

	/**
*
*@Title:transMap2Bean
*@param:@parammap
*@param:@paramobj
*@return:void
*@Description:Map-->Bean1:利用Introspector,PropertyDescriptor實現Map-->Bean
*@throws
*/
publicstaticvoidtransMap2Bean(Map<String,Object>map,Objectobj){

try{
BeanInfobeanInfo=Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[]propertyDescriptors=beanInfo.getPropertyDescriptors();

穗洞冊for(PropertyDescriptorproperty:propertyDescriptors){
Stringkey=property.getName();

顫禪if(map.containsKey(key)){
Objectvalue=map.get(key);
//得到property對應的setter方法
Methodsetter=property.getWriteMethod();
setter.invoke(obj,value);
}

}
猜宏
}catch(Exceptione){
System.out.println("transMap2BeanError"+e);
}

return;

}
閱讀全文

與c將json數組轉化成對象相關的資料

熱點內容
origin可以用哪些文件格式 瀏覽:841
python批量下載oa文件夾 瀏覽:488
xml文件怎麼輸入內容 瀏覽:392
三星手機加密文件夾 瀏覽:1000
cvi例子在哪個文件夾 瀏覽:18
好玩的蘋果商店塔防游戲排行榜 瀏覽:797
snow密碼錯誤怎麼辦 瀏覽:733
電腦主機如何存儲數據2年 瀏覽:456
學校網路是什麼模式 瀏覽:330
電腦微信config文件能刪除嘛 瀏覽:312
如何下載蘇州道app 瀏覽:382
網路接入服務商查詢 瀏覽:370
全球網大數據天眼系統是什麼 瀏覽:2
word2007顯示批註 瀏覽:177
xlsm宏文件如何使用 瀏覽:761
db2資料庫連接池如何重連 瀏覽:7
安卓蘋果換機傳文件 瀏覽:562
win10對話框不全 瀏覽:614
iphone4刪除不了照片 瀏覽:803
安卓faceriglive2d 瀏覽:736

友情鏈接