当前位置: 首页 > 面试题库 >

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

谭嘉歆
2023-03-14
问题内容

我的脚本向我返回了一个数组(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());
}

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

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

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

_JavaDoc中_的executeScript()提到了以下情况:

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

  • 对于HTML元素,此方法返回一个WebElement
  • 对于小数,返回一个Double
  • 对于非十进制数字,将返回Long
  • 对于布尔值,返回布尔值
  • 对于所有其他情况,将返回字符串
  • 对于数组,请按照上述规则返回一个List,其中的每个对象都应包含在内。我们支持嵌套列表。
  • 对于地图,请返回具有遵循上述规则的值的地图。
  • 除非该值为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(数组中的数组等),您可能必须先解析并保存内部数组,然后再解析外部数组。



 类似资料:
  • 问题内容: 我有一个想要转换为纯JavaScript数组的json数组: 这是我的json数组: 如何将其转换为普通的javascript数组,如下所示: 问题答案: 已经是JS对象(不是JSON)。但是,您可以在这里: 编辑:将 元素插入数组中的正确位置。谢谢@RoToRa 也许一开始不创建此类对象会更容易。它是如何创建的?

  • 问题内容: 我有一个包含几个整数的数组。我已经向数组添加了一些值,但是现在我需要通过jQuery的方法将此数组发送到页面。如何将其转换为JSON对象进行发送? 问题答案: 向后兼容的脚本:https : //github.com/douglascrockford/JSON- js/blob/master/json2.js 并致电: 注意: JSON对象现在是大多数现代Web浏览器(IE 8及更高版

  • 问题内容: 我有一些字典想要转换为JSON。我的对象是类型,并且最终要得到这样的示例: 这是我正在尝试的方法,但是编译器不喜欢我的声明: 我怎样才能做到这一点? 问题答案: 一种简单的实现方法是扩展CollectionType。 使用可选的绑定和向下转换,然后序列化为数据,然后转换为字符串。 输出:

  • 问题内容: 我有一个应用程序,我可以使用我在应用程序中使用的索引以数组&的形式从server(json)中获取数据,如下所示。 对于小尺寸的阵列,我可以得到这样的结果。但是当array包含’n’个元素时,我必须动态地使用它,那时它需要转换为StringArray。有人可以告诉我如何将json数组转换为String数组吗?谢谢 问题答案: 这应该对您有帮助。 编辑: 也许这就是您需要的:

  • 问题内容: myArray是: 我想将其转换为JSON,例如: 我试过了: 但这正在返回: 问题答案: 您的问题是非顺序索引。 Php数组必须具有要编码为JSON数组的索引。 您可以使用array_values重置数组键:

  • 问题内容: 我有一个包含几个整数的数组。我已经向数组添加了一些值,但是现在我需要通过jQuery的方法将此数组发送到页面。如何将其转换为JSON对象进行发送? 问题答案: 并致电: 注意: _JSON对象现在是大多数现代Web浏览器(IE 8及更高版本)的一部分。