[
{
"name": "Test",
"type": "Private",
"item":[{"itmeNo":"PT-15003C","quantity":"3"},
{"itmeNo":"PT-15003C","quantity":"3"}],
"successMsg":"Item(s) added to the job list."
}
]
你好,我正在使用json进行数据参数化。以上是我的json数据,我想在一个表格中输入这个数据,其中我需要输入itemNo和quantity在一起。如何使用json进行数据参数化。当有一个键-一个值对,然后我的代码工作,但在这种情况下,谁能帮助我得到解决方案?我为单个密钥对值编写了以下代码:
public static Object[][] getData(String path) {
JSONParser parser = new JSONParser();
JSONArray jArray = null;
Object[][] testData = null;
try {
jArray = (JSONArray) parser.parse(new FileReader(System.getProperty("user.dir") + path));
testData = new Object[jArray.size()][1];
Hashtable<String, String> table = new Hashtable<String, String>();
int i = 0;
for (Object obj : jArray) {
table = new Hashtable<String, String>();
JSONObject objJson = (JSONObject) obj;
Set<?> keys = objJson.keySet();
Iterator a = keys.iterator();
while (a.hasNext()) {
String key = (String) a.next();
String value = (String) objJson.get(key);
// System.out.print("key : "+key);
// System.out.println(" value :"+value);
table.put(key, value);
}
testData[i][0] = table;
i++;
}
return testData;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
这很简单。首先创建一个类,让我们说TestObject.java和Item.java,如下所示,以适应您的json结构。
TestObject.java
import java.util.List;
public class TestObject {
private String name;
private String type;
private List<Item> items;
private String successMsg;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the items
*/
public List<Item> getItems() {
return items;
}
/**
* @param items the items to set
*/
public void setItems(List<Item> items) {
this.items = items;
}
/**
* @return the successMsg
*/
public String getSuccessMsg() {
return successMsg;
}
/**
* @param successMsg the successMsg to set
*/
public void setSuccessMsg(String successMsg) {
this.successMsg = successMsg;
}
}
Item.java
public class Item {
private String itemNo;
private int qty;
/**
* @return the itemNo
*/
public String getItemNo() {
return itemNo;
}
/**
* @param itemNo the itemNo to set
*/
public void setItemNo(String itemNo) {
this.itemNo = itemNo;
}
/**
* @return the qty
*/
public int getQty() {
return qty;
}
/**
* @param qty the qty to set
*/
public void setQty(int qty) {
this.qty = qty;
}
}
现在解析json字符串,如下所示。下面是示例程序,它将帮助您了解基础知识:
Test.java
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) {
TestObject testObject = new TestObject();
try {
JSONObject jsonObject = new JSONObject("{"name": "Test","
+ ""type": "Private","
+ ""item":[{"itmeNo":"PT-15003C","quantity":"3"},"
+ " {"itmeNo":"PT-15003C","quantity":"3"}],"
+ ""successMsg":"Item(s) added to the job list."}"); //pass json string
JSONArray jsonArray = jsonObject.getJSONArray("item"); //get the item jsonarray
testObject.setName(jsonObject.getString("name"));
testObject.setType(jsonObject.getString("type"));
List<Item> items = new ArrayList<>();
Item item = null;
//Iterate the item array and add to the itemlist object
for (int i = 0; i < jsonArray.length() ; i++) {
JSONObject jsonItem = jsonArray.getJSONObject(i);
String itmeNo = jsonItem.getString("itmeNo");
String quantity = jsonItem.getString("quantity");
item = new Item();
item.setItemNo(itmeNo);
item.setQty(Integer.parseInt(quantity));
items.add(item);
}
testObject.setItems(items);
testObject.setSuccessMsg(jsonObject.getString("successMsg")); //Now you will have a testObject which have all values from json.
} catch (JSONException e) {
e.printStackTrace();
}
}
}