如何将数组(JSON数组)转换为Map?

时间:2018-03-17 12:49:57

标签: 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=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}]

我需要打印 Key / Pair 值,就像我从 Map 打印一样,如下所示:

Map<String, ?> map = cap.asMap();
for ( Map.Entry<String, ?> entry : map.entrySet()) 
{
    System.out.println(entry.getKey() + " value is " + entry.getValue());
}

我试图按照以下链接但仍然没有成功:

我收到的JSON格式是否可能与常规JSON格式不同?

我当前的代码基于 Selenium ,它使用 Javascript 来读取性能统计信息,如下所示:

String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();
System.out.println(netData);
executeScript()

JavaDocs 提及以下内容:

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

  • 对于HTML元素,此方法返回WebElement
  • 对于小数,返回Double
  • 对于非十进制数字,返回Long
  • 对于布尔值,返回布尔值
  • 对于所有其他情况,返回一个String。
  • 对于数组,按照上述规则返回包含每个对象的List。我们支持嵌套列表。
  • 对于地图,请按照上述规则返回包含值的地图。
  • 除非值为null或没有返回值,否则返回null

1 个答案:

答案 0 :(得分:3)

对于不那么复杂的jsons,你可以尝试手动操作......

首先删除{和[

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

如果你想要?作为你的地图数组,你必须自己从String转换为Int等...

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