【问题标题】:get value form json object从 json 对象中获取值
【发布时间】:2016-01-29 12:39:57
【问题描述】:

我有一个代码,它使用来自 db 的 Hibernate 获取对象的 List。对象的字段之一具有 XML 格式。我在jsp 文件中使用此代码获取该字段值:

for(PluginMonitor pm : list ) {
    // parsing xml to json
    JSONObject metricsFieldValue = org.json.XML.toJSONObject(pm.getMetrics());
    Object metricMetric = metricsFieldValue.get("metric");      
}

metricsFieldValue 的输出是

{
    "metric": [
        {
            "defaultThreshold": "0",
            "preselected": "true",
            "uom": "B/sec",
            "name": "BytesReceivedPersec",
            "type": "integer",
            "displayName": "Input Traffic"
        },
        {
            "defaultThreshold": "0",
            "preselected": "true",
            "uom": "B/sec",
            "name": "BytesSentPersec",
            "type": "integer",
            "displayName": "Output Traffic"
        }
    ]
}

所以metricMetric 的输出是一个数组,但我只需要该数组中每个对象的displayNamename。任何想法如何获得这些值。

【问题讨论】:

    标签: java json jsp


    【解决方案1】:

    尝试单独使用字符串;

    String displayName = jo.getJSONArray("metric").getJSONObject(i).getString("displayName");
    String name =  jo.getJSONArray("metric").getJSONObject(i).getString("name");
    

    【讨论】:

      【解决方案2】:

      JSONObject 有一个构造函数,可以从另一个JSONObject 中选择特定字段

      public JSONObject(JSONObject jo, java.lang.String[] names)
      

      从另一个 JSONObject 的子集构造一个 JSONObject。
      字符串数组用于标识应复制的键。
      缺少的键将被忽略。

      在你的情况下,你会像这样使用它:

      String[] fields = new String[] {"displayName", "name"};
      JSONObject onlyNames = new JSONObject(metricsFieldValue, fields);
      

      【讨论】:

        【解决方案3】:
        JSONArray metric = new JSONArray();
        metric = metricsFieldValue.getJSONArray("metric");
        for(int i=0; i< metric.length(); i++){
            JSONObject jsonObject = metric.getJSONObject(i);
            String displayName = jsonObject.get("displayName");
            String name = jsonObject.get("name");
        }
        

        如果你使用(导入)org.json.simple.JSONArray,你必须使用JSONArray.size() 来获取你想要的数据。但如果您使用的是org.json.JSONArray,请使用JSONArray.length()

        【讨论】:

          【解决方案4】:

          这就是它的工作原理

          List<PluginMonitor> list = new ArrayList<PluginMonitor>() ;
          list = instance.getPluginMonitorList();
          JSONObject metricsFieldValue = null;
          JSONObject jo = null;
          int listSize = list.size(); 
          int ln = 0;
          JSONObject json = null; 
          JSONArray ja = null;
          JSONObject jsonObj = null;  
          
          for(PluginMonitor pm: list){
              if(pm == null) {continue;}
              try{
                  json = org.json.XML.toJSONObject(pm.getMetrics());          
                  ja = json.getJSONArray("metric");
                  ln = ja.length();
                  for(int k = 0; k < ln; k++ ){
                      jsonObj = ja.getJSONObject(k);
                      out.print( "<H1> Name: " +jsonObj.get("name") + "</h1>");
                      out.print( "<H2 style='color: gray'> Display Name: " +jsonObj.get("displayName") + "</h2>"); 
                  }
              }catch(Exception e){
                  e.printStackTrace();
              }       
          }           
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-06-10
            • 2018-11-15
            • 2020-02-17
            • 2020-09-28
            相关资源
            最近更新 更多