【问题标题】:Fetch JSON values in java在java中获取JSON值
【发布时间】:2015-05-13 16:14:37
【问题描述】:

用于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;
        }

【问题讨论】:

    标签: java json org.json


    【解决方案1】:

    这是你的任务,你可以随意使用..

    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);
    
                }
    
    
            }
    
    
        }
    
    }
    

    希望对您有所帮助...!

    【讨论】:

      猜你喜欢
      • 2020-03-11
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多