从JSON数据中仅获取Object的一部分

时间:2018-01-15 05:26:00

标签: javascript jquery json

我在 JSON 数据

中仅将logo object的一部分定义为logoName时需要帮助
{  
   "2017-12-17":[  

   ],
   "2017-12-18":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":1,
         "nb_actions":5,
         "nb_users":0,
         "max_actions":5,
         "sum_visit_length":184,
         "bounce_count":0,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2017-12-19":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":7,
         "nb_actions":29,
         "nb_users":0,
         "max_actions":15,
         "sum_visit_length":4195,
         "bounce_count":2,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      },
      {  
         "label":"Mobile Safari",
         "nb_uniq_visitors":2,
         "nb_visits":2,
         "nb_actions":2,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":2,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/MF.png",
         "segment":"browserCode==MF"
      }
   ],
   "2017-12-20":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":1,
         "nb_actions":4,
         "nb_users":0,
         "max_actions":4,
         "sum_visit_length":794,
         "bounce_count":0,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2017-12-21":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":1,
         "nb_actions":14,
         "nb_users":0,
         "max_actions":14,
         "sum_visit_length":287,
         "bounce_count":0,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2017-12-22":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":2,
         "nb_actions":9,
         "nb_users":0,
         "max_actions":7,
         "sum_visit_length":219,
         "bounce_count":0,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2017-12-23":[  

   ],
   "2017-12-24":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":1,
         "nb_actions":12,
         "nb_users":0,
         "max_actions":12,
         "sum_visit_length":480,
         "bounce_count":0,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2017-12-25":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":3,
         "nb_actions":4,
         "nb_users":0,
         "max_actions":2,
         "sum_visit_length":7,
         "bounce_count":2,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2017-12-26":[  

   ],
   "2017-12-27":[  
      {  
         "label":"Mobile Safari",
         "nb_uniq_visitors":9,
         "nb_visits":9,
         "nb_actions":9,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":9,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/MF.png",
         "segment":"browserCode==MF"
      },
      {  
         "label":"Android Browser",
         "nb_uniq_visitors":4,
         "nb_visits":4,
         "nb_actions":4,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":4,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/AN.png",
         "segment":"browserCode==AN"
      },
      {  
         "label":"Internet Explorer",
         "nb_uniq_visitors":4,
         "nb_visits":4,
         "nb_actions":4,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":4,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/IE.png",
         "segment":"browserCode==IE"
      },
      {  
         "label":"Chrome",
         "nb_uniq_visitors":3,
         "nb_visits":3,
         "nb_actions":3,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":3,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      },
      {  
         "label":"Chrome Mobile",
         "nb_uniq_visitors":2,
         "nb_visits":2,
         "nb_actions":2,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":2,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CM.png",
         "segment":"browserCode==CM"
      },
      {  
         "label":"Firefox",
         "nb_uniq_visitors":2,
         "nb_visits":2,
         "nb_actions":2,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":2,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/FF.png",
         "segment":"browserCode==FF"
      },
      {  
         "label":"Opera",
         "nb_uniq_visitors":1,
         "nb_visits":1,
         "nb_actions":1,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":1,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/OP.png",
         "segment":"browserCode==OP"
      },
      {  
         "label":"Safari",
         "nb_uniq_visitors":1,
         "nb_visits":1,
         "nb_actions":1,
         "nb_users":0,
         "max_actions":1,
         "sum_visit_length":0,
         "bounce_count":1,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/SF.png",
         "segment":"browserCode==SF"
      }
   ],
   "2017-12-28":[  

   ],
   "2017-12-29":[  

   ],
   "2017-12-30":[  

   ],
   "2017-12-31":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":1,
         "nb_actions":6,
         "nb_users":0,
         "max_actions":6,
         "sum_visit_length":41,
         "bounce_count":0,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2018-01-01":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":2,
         "nb_actions":9,
         "nb_users":0,
         "max_actions":7,
         "sum_visit_length":103,
         "bounce_count":0,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2018-01-02":[  

   ],
   "2018-01-03":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":3,
         "nb_actions":6,
         "nb_users":0,
         "max_actions":3,
         "sum_visit_length":250,
         "bounce_count":1,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ],
   "2018-01-04":[  

   ],
   "2018-01-05":[  

   ]
}

如何仅定义文件名,例如在上面的 JSON 中,Chrome浏览器的logo文件名为CH.png

我想获取object,但只返回我在JS中使用的文件名

logo.src = 'https://example.com/img/browser-icons/' + logoName + '.png';

或者,可以使用object segment完成相同操作,例如CH返回browserCode==CH

我的完整代码将JSON数据显示为表



$.getJSON('https://discovrbookings.innocraft.cloud/index.php?module=API&method=DevicesDetection.getBrowsers&idSite=2&period=day&date=2017-12-17,2018-01-05&format=json&token_auth=68aa5bd12137f13255dcb98794b65dff', (browser_data) => {
  Array.prototype.groupBy = function(key) {
    var path = key.split('.');
    var result = {};
    try {
      this.forEach(function(item) {
        // es6: path.reduce((a, b) => a[b], item)
        type = path.reduce(function(a, b) {
          return a[b]
        }, item) || 'null';
        if (!result[type])
          result[type] = [];
        result[type].push(item);
      });
      return result;
    } catch (err) {
      console.log(err);
      return {};
    }
  };

  function getPropertySum(key, arr) {
    return arr.reduce((a, b) => (key in b ? a + b[key] : a), 0)
  }
  // one array of all dates
  let browserCode = ('segment');
  let flattenArr = [].concat.apply([], Object.values(browser_data));
  // object of grouped dates by device
  let groups = flattenArr.groupBy('label');
  let table = document.getElementById('browserTable');
  table.innerHTML = '';
  Object.keys(groups).forEach(function(label) {
    let row = document.createElement('tr');
    let logw = document.createElement('td');
    let logo = document.createElement('img');
    let lab = document.createElement('td');
    let nbv = document.createElement('td');
    let nbu = document.createElement('td');
    row.appendChild(logw);
    row.appendChild(lab);
    row.appendChild(nbv);
    row.appendChild(nbu);
    logw.appendChild(logo);
    logo.src = 'https://discovrbookings.com/wp-content/themes/discovr-application/assets/img/browser-icons/' + logoName + '.png';
    lab.innerHTML = label;
    nbv.innerHTML = getPropertySum('nb_visits', groups[label]);
    nbu.innerHTML = getPropertySum('nb_uniq_visitors', groups[label]);
    table.appendChild(row);
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="browserTable"></table>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您需要获取徽标字符串获取最后一部分,并将文件名分配给变量private void Load_To_TableActionPerformed(java.awt.event.ActionEvent evt) { try{ Statement s = DB.getConnection().createStatement(); DefaultTableModel dtm=(DefaultTableModel)tb1.getModel(); Vector v=new Vector(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ResultSet rs,rs1,rs2,rs3,rs4,rs5; rs1=j.getData("select E_ID from User where E_ID='"+user+"'"); if(rs1.next()==true) { rs=j.getData("select * from LCI where L_DATE= '"+dateFormat.format(DATE.getDate())+"'"); if(rs.next()== true) { v.add(rs.getString("L_ID")); v.add(rs.getString("HEADING")); v.add(rs.getString("INSTITUTE")); v.add(rs.getString("L_DATE")); JOptionPane.showMessageDialog(this, "Successful"); } else { System.out.println(rs.getString("L_DATE")); JOptionPane.showMessageDialog(this, "There is no data related to the date you entered.Please try another date."); } } else{ JOptionPane.showMessageDialog(this, "Failed to connect with DB. Please try again"); } } catch(Exception ex) { ex.printStackTrace(); } // TODO add your handling code here: } 。我在代码底部添加了一个代码段。

&#13;
&#13;
result set
&#13;
java.sql.SQLException: Illegal operation on empty result set.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:855)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5773)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5733)
    at Viewer.LCI.Load_To_TableActionPerformed(LCI.java:668)
    at Viewer.LCI.access$800(LCI.java:25)
    at Viewer.LCI$9.actionPerformed(LCI.java:342)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:718)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 8 minutes 7 seconds)
&#13;
&#13;
&#13;

答案 1 :(得分:0)

要获取文件名,您可.split() logo属性值"/".pop()数组

let url = "plugins\/Morpheus\/icons\/dist\/browsers\/CH.png";

let logoName = url.split("/").pop();

答案 2 :(得分:0)

为什么不使用Object.keys来遍历大型JSON对象?

var data = {  
   "2017-12-17":[  

   ],
   "2017-12-18":[  
      {  
         "label":"Chrome",
         "nb_uniq_visitors":1,
         "nb_visits":1,
         "nb_actions":5,
         "nb_users":0,
         "max_actions":5,
         "sum_visit_length":184,
         "bounce_count":0,
         "nb_visits_converted":0,
         "logo":"plugins\/Morpheus\/icons\/dist\/browsers\/CH.png",
         "segment":"browserCode==CH"
      }
   ]
}
var keys = Object.keys(data); // this will output ['2017-12-17','2017-12-18',..]



 keys.forEach(function(d){ 
    data[d].forEach(function(i){
        console.log(i.logo);
    });
});

输出

  

插件/睡眠/图标/ DIST /浏览器/ CH.png

     

插件/睡眠/图标/ DIST /浏览器/ MF.png

     

插件/睡眠/图标/ DIST /浏览器/ CH.png

     

插件/睡眠/图标/ DIST /浏览器/ MF.png

     

插件/睡眠/图标/ DIST /浏览器/ AN.png

     

插件/睡眠/图标/ DIST /浏览器/ IE.png

     

插件/睡眠/图标/ DIST /浏览器/ CH.png

     

插件/睡眠/图标/ DIST /浏览器/ CM.png

     

插件/睡眠/图标/ DIST /浏览器/ FF.png

     

插件/睡眠/图标/ DIST /浏览器/ OP.png

     

插件/睡眠/图标/ DIST /浏览器/ SF.png

     

插件/睡眠/图标/ DIST /浏览器/ CH.png