Wednesday 22 February 2017

javascript - How to convert a Array (JSON Array) to Map?




My script returns me a Array (JSON Array) as follows :



[{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<=-66418&ln=-307466&zx=1521289508660, startTime=3044.899999993504, fetchStart=3044.899999993504, serverTiming=[], nextHopProtocol=h2, initiatorType=beacon}]


I need to print the Key / Pair values just like I print from a Map as follows :



Map map = cap.asMap();
for ( Map.Entry entry : map.entrySet())
{

System.out.println(entry.getKey() + " value is " + entry.getValue());
}


I have tried to follow the following links but still no success:





Is it possible the JSON format I am recieving is different from the regular JSON formats?




My current code is based on Selenium which uses a Javascript to read the performance statistics as follows :



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


JavaDocs of executeScript() mentions the following :



If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:





  • For an HTML element, this method returns a WebElement

  • For a decimal, a Double is returned

  • For a non-decimal number, a Long is returned

  • For a boolean, a Boolean is returned

  • For all other cases, a String is returned.

  • For an array, return a List with each object following the rules above. We support nested lists.

  • For a map, return a Map with values following the rules above.

  • Unless the value is null or there is no return value, in which null is returned



Answer



for not so complicated jsons, you can try doing it manually...



first remove { and [



jsontext = jsontext.replaceAll ("[\\[\\]{}]", "");


then split the json array using the , separator




String[] items = jsontext.split(",");


then for each item, split using the = separator, and then populate into your array



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


if you want ? as your map array, then you have to do your own conversion from String to Int, etc...




for more complicated jsons (arrays within arrays, etc), you may have to resolve and save the inner arrays first, then resolve the outer arrays.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...