Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将数组(JSON数组)转换为映射?_Javascript_Java_Json_Selenium_Collections - Fatal编程技术网

Javascript 如何将数组(JSON数组)转换为映射?

Javascript 如何将数组(JSON数组)转换为映射?,javascript,java,json,selenium,collections,Javascript,Java,Json,Selenium,Collections,我的脚本返回一个数组(JSON数组),如下所示: [{redirectCount=0, encodedBodySize=60962, unloadEventEnd=0, responseEnd=1601.699999999255, domainLookupEnd=995.7999999896856, unloadEventStart=0, domContentLoadedEventStart=1970.3999999910593, type=navigate, decodedBodySize=2

我的脚本返回一个数组(JSON数组),如下所示:

[{redirectCount=0, encodedBodySize=60962, unloadEventEnd=0, responseEnd=1601.699999999255, domainLookupEnd=995.7999999896856, unloadEventStart=0, domContentLoadedEventStart=1970.3999999910593, type=navigate, decodedBodySize=215675, duration=3103.799999997136, redirectStart=0, connectEnd=1357.1999999985565, toJSON={}, requestStart=1359.599999996135, startTime=0, fetchStart=1598.6999999877298, serverTiming=[], domContentLoadedEventEnd=1981.999999989057, entryType=navigation, workerStart=0, responseStart=1499.7000000003027, domInteractive=1970.2999999863096, domComplete=3093.799999987823, domainLookupStart=995.7999999896856, redirectEnd=0, transferSize=65141, connectStart=995.7999999896856, loadEventStart=3094.4999999919673, secureConnectionStart=1041.6999999870313, name=http://www.google.com/, nextHopProtocol=h2, initiatorType=navigation, loadEventEnd=3103.799999997136}, {encodedBodySize=5087, entryType=resource, responseEnd=1782.099999996717, workerStart=0, responseStart=1732.5999999884516, domainLookupEnd=1646.7999999877065, domainLookupStart=1646.7999999877065, redirectEnd=0, decodedBodySize=5087, duration=135.30000000901055, transferSize=5262, redirectStart=0, connectEnd=1646.7999999877065, toJSON={}, connectStart=1646.7999999877065, requestStart=1648.1999999959953, secureConnectionStart=0, name=https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png, startTime=1646.7999999877065, fetchStart=1646.7999999877065, serverTiming=[], nextHopProtocol=h2, initiatorType=img}, {encodedBodySize=5969, entryType=resource, responseEnd=1919.0999999991618, workerStart=0, responseStart=1777.9999999911524, domainLookupEnd=1647.0999999874039, domainLookupStart=1647.0999999874039, redirectEnd=0, decodedBodySize=5969, duration=272.00000001175795, transferSize=6039, redirectStart=0, connectEnd=1647.0999999874039, toJSON={}, connectStart=1647.0999999874039, requestStart=1648.6999999906402, secureConnectionStart=0, name=https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png, startTime=1647.0999999874039, fetchStart=1647.0999999874039, serverTiming=[], nextHopProtocol=h2, initiatorType=img}, {encodedBodySize=0, entryType=resource, responseEnd=1985.7999999949243, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=320.5000000016298, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://ssl.gstatic.com/gb/images/i1_1967ca6a.png, startTime=1665.2999999932945, fetchStart=1665.2999999932945, serverTiming=[], nextHopProtocol=h2, initiatorType=css}, {encodedBodySize=0, entryType=resource, responseEnd=2168.599999989965, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=387.09999999264255, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://www.google.com/gen_204?dml=1, startTime=1781.4999999973224, fetchStart=1781.4999999973224, serverTiming=[], nextHopProtocol=h2, initiatorType=img}, {duration=0, entryType=paint, toJSON={}, name=first-paint, startTime=1781.5999999875203}, {duration=0, entryType=paint, toJSON={}, name=first-contentful-paint, startTime=1781.5999999875203}, {encodedBodySize=0, entryType=resource, responseEnd=2054.49999999837, workerStart=0, responseStart=2053.599999999278, domainLookupEnd=1969.699999986915, domainLookupStart=1969.699999986915, redirectEnd=0, decodedBodySize=0, duration=84.80000001145527, transferSize=56, redirectStart=0, connectEnd=1969.699999986915, toJSON={}, connectStart=1969.699999986915, requestStart=1970.7999999955064, secureConnectionStart=0, name=https://www.google.co.in/gen_204?s=webaft&atyp=csi&ei=JwmtWtvfK4jrvgSd2rSoDA&rt=wsrt.1622,aft.346,prt.346, startTime=1969.699999986915, fetchStart=1969.699999986915, serverTiming=[], nextHopProtocol=h2, initiatorType=beacon}, {encodedBodySize=150145, entryType=resource, responseEnd=2339.9999999965075, workerStart=0, responseStart=2215.3999999864027, domainLookupEnd=1985.000000000582, domainLookupStart=1985.000000000582, redirectEnd=0, decodedBodySize=432554, duration=354.99999999592546, transferSize=150402, redirectStart=0, connectEnd=1985.000000000582, toJSON={}, connectStart=1985.000000000582, requestStart=2167.799999995623, secureConnectionStart=0, name=https://www.google.co.in/xjs/_/js/k=xjs.s.en.ruBw46222DE.O/m=sx,sb,cdos,cr,elog,hsm,jsa,r,d,csi/am=wCJ0xz8A-f_BgCLRCkZYgGjBMDQ/rt=j/d=1/t=zcms/rs=ACT90oHcVyvrSVGpTIcAIyAYtf5uuQMgeg, startTime=1985.000000000582, fetchStart=1985.000000000582, serverTiming=[], nextHopProtocol=h2, initiatorType=script}, {encodedBodySize=0, entryType=resource, responseEnd=2543.20000000007, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=358.0000000074506, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://www.gstatic.com/og/_/js/k=og.og2.en_US.WC6JeWv9ETU.O/rt=j/m=def/exm=in,fot/d=1/ed=1/rs=AA2YrTu6Da9cmMJFbNF2zgt8mvkJOB8gGQ, startTime=2185.1999999926193, fetchStart=2185.1999999926193, serverTiming=[], nextHopProtocol=h2, initiatorType=script}, {encodedBodySize=33183, entryType=resource, responseEnd=2536.19999998773, workerStart=0, responseStart=2515.89999999851, domainLookupEnd=2451.3000000006286, domainLookupStart=2451.3000000006286, redirectEnd=0, decodedBodySize=100255, duration=84.89999998710118, transferSize=33295, redirectStart=0, connectEnd=2451.3000000006286, toJSON={}, connectStart=2451.3000000006286, requestStart=2461.4000000001397, secureConnectionStart=0, name=https://www.google.co.in/xjs/_/js/k=xjs.s.en.ruBw46222DE.O/m=aa,abd,async,dvl,foot,fpe,ipv6,lu,m,mu,sf,sonic,spch,d3l,udlg/am=wCJ0xz8A-f_BgCLRCkZYgGjBMDQ/exm=sx,sb,cdos,cr,elog,hsm,jsa,r,d,csi/rt=j/d=1/ed=1/t=zcms/rs=ACT90oHcVyvrSVGpTIcAIyAYtf5uuQMgeg?xjs=s1, startTime=2451.3000000006286, fetchStart=2451.3000000006286, serverTiming=[], nextHopProtocol=h2, initiatorType=script}, {encodedBodySize=0, entryType=resource, responseEnd=2666.6999999870313, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=165.79999998793937, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://www.google.com/textinputassistant/tia.png, startTime=2500.899999999092, fetchStart=2500.899999999092, serverTiming=[], nextHopProtocol=h2, initiatorType=img}, {encodedBodySize=0, entryType=resource, responseEnd=3038.4999999951106, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=345.1000000059139, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://apis.google.com/_/scs/abc-static/_/js/k=gapi.gapi.en.KGfejEJNA7c.O/m=gapi_iframes,googleapis_client,plusone/rt=j/sv=1/d=1/ed=1/am=AAE/rs=AHpOoo8sfkynV0sdMp9PqJxnrfktogrWdA/cb=gapi.loaded_0, startTime=2693.3999999891967, fetchStart=2693.3999999891967, serverTiming=[], nextHopProtocol=h2, initiatorType=script}, {encodedBodySize=0, entryType=resource, responseEnd=3129.799999995157, workerStart=0, responseStart=3129.3000000005122, domainLookupEnd=3044.899999993504, domainLookupStart=3044.899999993504, redirectEnd=0, decodedBodySize=0, duration=84.9000000016531, transferSize=54, redirectStart=0, connectEnd=3044.899999993504, toJSON={}, connectStart=3044.899999993504, requestStart=3046.8999999866355, secureConnectionStart=0, name=https://www.google.co.in/gen_204?atyp=i&ct=&cad=udla=1&ei=JwmtWtvfK4jrvgSd2rSoDA&di=3325&lt=-66418&ln=-307466&zx=1521289508660, startTime=3044.899999993504, fetchStart=3044.899999993504, serverTiming=[], nextHopProtocol=h2, initiatorType=beacon}]
Map<String, ?> map = cap.asMap();
for ( Map.Entry<String, ?> entry : map.entrySet()) 
{
    System.out.println(entry.getKey() + " value is " + entry.getValue());
}
String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();
System.out.println(netData);
我需要打印密钥/密钥对值,就像从映射打印一样,如下所示:

[{redirectCount=0, encodedBodySize=60962, unloadEventEnd=0, responseEnd=1601.699999999255, domainLookupEnd=995.7999999896856, unloadEventStart=0, domContentLoadedEventStart=1970.3999999910593, type=navigate, decodedBodySize=215675, duration=3103.799999997136, redirectStart=0, connectEnd=1357.1999999985565, toJSON={}, requestStart=1359.599999996135, startTime=0, fetchStart=1598.6999999877298, serverTiming=[], domContentLoadedEventEnd=1981.999999989057, entryType=navigation, workerStart=0, responseStart=1499.7000000003027, domInteractive=1970.2999999863096, domComplete=3093.799999987823, domainLookupStart=995.7999999896856, redirectEnd=0, transferSize=65141, connectStart=995.7999999896856, loadEventStart=3094.4999999919673, secureConnectionStart=1041.6999999870313, name=http://www.google.com/, nextHopProtocol=h2, initiatorType=navigation, loadEventEnd=3103.799999997136}, {encodedBodySize=5087, entryType=resource, responseEnd=1782.099999996717, workerStart=0, responseStart=1732.5999999884516, domainLookupEnd=1646.7999999877065, domainLookupStart=1646.7999999877065, redirectEnd=0, decodedBodySize=5087, duration=135.30000000901055, transferSize=5262, redirectStart=0, connectEnd=1646.7999999877065, toJSON={}, connectStart=1646.7999999877065, requestStart=1648.1999999959953, secureConnectionStart=0, name=https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png, startTime=1646.7999999877065, fetchStart=1646.7999999877065, serverTiming=[], nextHopProtocol=h2, initiatorType=img}, {encodedBodySize=5969, entryType=resource, responseEnd=1919.0999999991618, workerStart=0, responseStart=1777.9999999911524, domainLookupEnd=1647.0999999874039, domainLookupStart=1647.0999999874039, redirectEnd=0, decodedBodySize=5969, duration=272.00000001175795, transferSize=6039, redirectStart=0, connectEnd=1647.0999999874039, toJSON={}, connectStart=1647.0999999874039, requestStart=1648.6999999906402, secureConnectionStart=0, name=https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png, startTime=1647.0999999874039, fetchStart=1647.0999999874039, serverTiming=[], nextHopProtocol=h2, initiatorType=img}, {encodedBodySize=0, entryType=resource, responseEnd=1985.7999999949243, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=320.5000000016298, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://ssl.gstatic.com/gb/images/i1_1967ca6a.png, startTime=1665.2999999932945, fetchStart=1665.2999999932945, serverTiming=[], nextHopProtocol=h2, initiatorType=css}, {encodedBodySize=0, entryType=resource, responseEnd=2168.599999989965, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=387.09999999264255, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://www.google.com/gen_204?dml=1, startTime=1781.4999999973224, fetchStart=1781.4999999973224, serverTiming=[], nextHopProtocol=h2, initiatorType=img}, {duration=0, entryType=paint, toJSON={}, name=first-paint, startTime=1781.5999999875203}, {duration=0, entryType=paint, toJSON={}, name=first-contentful-paint, startTime=1781.5999999875203}, {encodedBodySize=0, entryType=resource, responseEnd=2054.49999999837, workerStart=0, responseStart=2053.599999999278, domainLookupEnd=1969.699999986915, domainLookupStart=1969.699999986915, redirectEnd=0, decodedBodySize=0, duration=84.80000001145527, transferSize=56, redirectStart=0, connectEnd=1969.699999986915, toJSON={}, connectStart=1969.699999986915, requestStart=1970.7999999955064, secureConnectionStart=0, name=https://www.google.co.in/gen_204?s=webaft&atyp=csi&ei=JwmtWtvfK4jrvgSd2rSoDA&rt=wsrt.1622,aft.346,prt.346, startTime=1969.699999986915, fetchStart=1969.699999986915, serverTiming=[], nextHopProtocol=h2, initiatorType=beacon}, {encodedBodySize=150145, entryType=resource, responseEnd=2339.9999999965075, workerStart=0, responseStart=2215.3999999864027, domainLookupEnd=1985.000000000582, domainLookupStart=1985.000000000582, redirectEnd=0, decodedBodySize=432554, duration=354.99999999592546, transferSize=150402, redirectStart=0, connectEnd=1985.000000000582, toJSON={}, connectStart=1985.000000000582, requestStart=2167.799999995623, secureConnectionStart=0, name=https://www.google.co.in/xjs/_/js/k=xjs.s.en.ruBw46222DE.O/m=sx,sb,cdos,cr,elog,hsm,jsa,r,d,csi/am=wCJ0xz8A-f_BgCLRCkZYgGjBMDQ/rt=j/d=1/t=zcms/rs=ACT90oHcVyvrSVGpTIcAIyAYtf5uuQMgeg, startTime=1985.000000000582, fetchStart=1985.000000000582, serverTiming=[], nextHopProtocol=h2, initiatorType=script}, {encodedBodySize=0, entryType=resource, responseEnd=2543.20000000007, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=358.0000000074506, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://www.gstatic.com/og/_/js/k=og.og2.en_US.WC6JeWv9ETU.O/rt=j/m=def/exm=in,fot/d=1/ed=1/rs=AA2YrTu6Da9cmMJFbNF2zgt8mvkJOB8gGQ, startTime=2185.1999999926193, fetchStart=2185.1999999926193, serverTiming=[], nextHopProtocol=h2, initiatorType=script}, {encodedBodySize=33183, entryType=resource, responseEnd=2536.19999998773, workerStart=0, responseStart=2515.89999999851, domainLookupEnd=2451.3000000006286, domainLookupStart=2451.3000000006286, redirectEnd=0, decodedBodySize=100255, duration=84.89999998710118, transferSize=33295, redirectStart=0, connectEnd=2451.3000000006286, toJSON={}, connectStart=2451.3000000006286, requestStart=2461.4000000001397, secureConnectionStart=0, name=https://www.google.co.in/xjs/_/js/k=xjs.s.en.ruBw46222DE.O/m=aa,abd,async,dvl,foot,fpe,ipv6,lu,m,mu,sf,sonic,spch,d3l,udlg/am=wCJ0xz8A-f_BgCLRCkZYgGjBMDQ/exm=sx,sb,cdos,cr,elog,hsm,jsa,r,d,csi/rt=j/d=1/ed=1/t=zcms/rs=ACT90oHcVyvrSVGpTIcAIyAYtf5uuQMgeg?xjs=s1, startTime=2451.3000000006286, fetchStart=2451.3000000006286, serverTiming=[], nextHopProtocol=h2, initiatorType=script}, {encodedBodySize=0, entryType=resource, responseEnd=2666.6999999870313, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=165.79999998793937, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://www.google.com/textinputassistant/tia.png, startTime=2500.899999999092, fetchStart=2500.899999999092, serverTiming=[], nextHopProtocol=h2, initiatorType=img}, {encodedBodySize=0, entryType=resource, responseEnd=3038.4999999951106, workerStart=0, responseStart=0, domainLookupEnd=0, domainLookupStart=0, redirectEnd=0, decodedBodySize=0, duration=345.1000000059139, transferSize=0, redirectStart=0, connectEnd=0, toJSON={}, connectStart=0, requestStart=0, secureConnectionStart=0, name=https://apis.google.com/_/scs/abc-static/_/js/k=gapi.gapi.en.KGfejEJNA7c.O/m=gapi_iframes,googleapis_client,plusone/rt=j/sv=1/d=1/ed=1/am=AAE/rs=AHpOoo8sfkynV0sdMp9PqJxnrfktogrWdA/cb=gapi.loaded_0, startTime=2693.3999999891967, fetchStart=2693.3999999891967, serverTiming=[], nextHopProtocol=h2, initiatorType=script}, {encodedBodySize=0, entryType=resource, responseEnd=3129.799999995157, workerStart=0, responseStart=3129.3000000005122, domainLookupEnd=3044.899999993504, domainLookupStart=3044.899999993504, redirectEnd=0, decodedBodySize=0, duration=84.9000000016531, transferSize=54, redirectStart=0, connectEnd=3044.899999993504, toJSON={}, connectStart=3044.899999993504, requestStart=3046.8999999866355, secureConnectionStart=0, name=https://www.google.co.in/gen_204?atyp=i&ct=&cad=udla=1&ei=JwmtWtvfK4jrvgSd2rSoDA&di=3325&lt=-66418&ln=-307466&zx=1521289508660, startTime=3044.899999993504, fetchStart=3044.899999993504, serverTiming=[], nextHopProtocol=h2, initiatorType=beacon}]
Map<String, ?> map = cap.asMap();
for ( Map.Entry<String, ?> entry : map.entrySet()) 
{
    System.out.println(entry.getKey() + " value is " + entry.getValue());
}
String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();
System.out.println(netData);
的JavaDocs提到了以下内容:

如果脚本具有返回值(即,如果脚本包含return语句),则将采取以下步骤:

  • 对于HTML元素,此方法返回WebElement
  • 对于十进制,返回一个双精度
  • 对于非十进制数,返回一个Long
  • 对于布尔值,返回一个布尔值
  • 对于所有其他情况,将返回一个字符串
  • 对于数组,返回一个列表,其中每个对象都遵循上述规则。我们支持嵌套列表
  • 对于映射,返回值符合上述规则的映射
  • 除非值为null或没有返回值,否则返回null

    • 对于不太复杂的JSON,您可以尝试手动操作

      首先删除{和[

      jsontext = jsontext.replaceAll ("[\\[\\]{}]", "");
      
      然后使用分隔符拆分json数组

      String[] items = jsontext.split(",");
      
      然后,对于每个项目,使用=分隔符拆分,然后填充到数组中

      Map<String, String> array;
      
      for (String s: items){
      
          String[] item = s.split("=");
          if(item.length == 2){
              array.put(item[0].trim(), item[1].trim());
          }else{
              System.out.println("Error with: "+ s);
          }
      }
      
      Map数组;
      用于(字符串s:项){
      字符串[]项=s.split(“”);
      如果(item.length==2){
      数组.put(项[0].trim(),项[1].trim());
      }否则{
      System.out.println(“错误为:+s”);
      }
      }
      
      如果你想?作为你的映射数组,那么你必须自己做从字符串到Int的转换,等等


      对于更复杂的JSON(数组中的数组等),您可能需要先解析并保存内部数组,然后解析外部数组。

      脚本返回的json格式无效。键值分隔符应该是
      而不是
      =
      。请重新查看问题中的数组。它包含
      toJSON={}
      。我还是不知道。