gpt4 book ai didi

java - 在java中获取JSON值

转载 作者:太空宇宙 更新时间:2023-11-04 13:51:53 27 4
gpt4 key购买 nike

用于json解析和迭代的API:

  • org.json.simple
  • org.json

我正在尝试从 ArchiveBatchChangeRequest -> DocumentSets -> QuerySpecification 下的 FieldName 获取两个值,并将其作为 ArrayList 返回。

  • 元素节点 =“ArchiveBatchChangeRequest;DocumentSets;QuerySpecification”
  • objectValue = "字段名称"
  • batchFile = JSON 输入文件

因此返回值应包含 CustomerName 和 DateCreated,因为 QuerySpecification 是一个大小为 2 的数组

我似乎无法让这个工作。恐怕我的初始遍历点非常糟糕,或者可以更容易地完成。

JSON 输入文件:

{
"ArchiveBatchChangeRequest": [{
"BatchRunSpecification": [{
"BatchOperation": "EXTRACT"
}]
},
{
"OutputSpecification": [{
"ReportFieldNames": ["ReportField1"]
}]
},
{
"DocumentSets": [{
"DocumentSetNo": "1",
"QuerySpecification": [{
"FieldName": "CustomerName",
"SimpleQuery": [{
"Operator": "EQUAL",
"Values": ["Customer1"]
}]
},
{
"FieldName": "DateCreated",
"SimpleQuery": [{
"Operator": "EQUAL",
"Values": ["19-12-2015"]
}]
}]
}]
}]
}

代码:

        public ArrayList<String> getElementValue(String elementNode, String objectValue, File batchFile) throws IOException, ParseException{
String [] elementArray = elementNode.split(";");
JSONObject outerObject = null;
ArrayList<String> values = new ArrayList<String>();
try {
JSONParser parser = new JSONParser();
Object object = parser.parse(new FileReader(batchFile));
outerObject = new JSONObject(object.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

JSONArray getArray = null;

for (int i = 0; i < elementArray.length; i++) {
getArray = outerObject.getJSONArray(elementArray[i]);

for (int j = 0; j < getArray.length(); j++) {
for (int j2 = 0; j2 < elementArray.length; j2++) {
if(getArray.getJSONObject(j).names().get(0).equals(elementArray[j2])){
outerObject = getArray.getJSONObject(j);
}
}
}

if(i == elementArray.length - 1){
outerObject = getArray.getJSONObject(0);
}
}

try{
getArray = null;
getArray = outerObject.getJSONArray(objectValue);
}catch(JSONException je){ //Object is not an JSONArray
je.printStackTrace();
}finally{
if(getArray != null){
for (int i = 0; i < getArray.length(); i++) {
values.add(getArray.getString(i).replace("\"", ""));
}
}else
values.add(outerObject.getString(objectValue));
}


return values;
}

最佳答案

这是您的任务并根据您的需要使用..

public void parse(String data) throws JSONException{  
org.json.JSONObject jsonObject=new org.json.JSONObject(data);
JSONArray array=jsonObject.getJSONArray("ArchiveBatchChangeRequest");
for(int i=0;i<array.length();i++){
org.json.JSONObject jsonObject2=array.getJSONObject(i);
if(i==0){
JSONArray array1=jsonObject2.getJSONArray("BatchRunSpecification");
org.json.JSONObject jsonObject3=array1.getJSONObject(0);
System.out.println("BatchOperation="+jsonObject3.get("BatchOperation"));
}else if(i==1){
JSONArray array1=jsonObject2.getJSONArray("OutputSpecification");
org.json.JSONObject jsonObject3=array1.getJSONObject(0);

JSONArray array2=jsonObject3.getJSONArray("ReportFieldNames");

System.out.println("ReportFieldNames="+array2.getString(0));
}
else if(i==2){

JSONArray array1=jsonObject2.getJSONArray("DocumentSets");
org.json.JSONObject jsonObject3=array1.getJSONObject(0);
System.out.println("DocumentSetNo="+jsonObject3.getString("DocumentSetNo"));
JSONArray array2=jsonObject3.getJSONArray("QuerySpecification");
for(int j=0;j< array2.length();j++){

org.json.JSONObject jobject=array2.getJSONObject(j);
String fieldName=jobject.getString("FieldName");
System.out.println("value fo FieldName use as you want"+fieldName);
String operator=jobject.getJSONArray("SimpleQuery").getJSONObject(0).getString("Operator");
System.out.println("value fo operator use as you want-"+operator);
String values=jobject.getJSONArray("SimpleQuery").getJSONObject(0).getJSONArray("Values").getString(0);
System.out.println("value fo values use as you want-"+values);

}


}


}

}

希望对您有帮助...!

关于java - 在java中获取JSON值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30220215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com