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

2024-01-02

我的脚本返回一个数组(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值就像我从a打印的Map如下 :

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

我尝试点击以下链接,但仍然没有成功:

  • 如何打印 json 对象和数组值? https://stackoverflow.com/questions/23733020/how-can-i-print-json-objects-and-array-values
  • 如何将从控制器类返回的 JSON 数组打印到 jsp 页面 https://stackoverflow.com/questions/35501208/how-to-print-json-array-returned-from-controller-class-to-jsp-page
  • JSON - 迭代 JSONArray https://stackoverflow.com/questions/6697147/json-iterate-through-jsonarray

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

我当前的代码基于Selenium它使用一个JavaScript读取性能统计信息如下:

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

JavaDocs of executeScript() https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html#executeScript-java.lang.String-java.lang.Object...-提到以下内容:

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

  • 对于 HTML 元素,此方法返回一个 WebElement
  • 对于小数,返回 Double
  • 对于非十进制数,返回 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);
    }
}

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将数组(JSON数组)转换为Map? 的相关文章

随机推荐