{"id":863,"date":"2015-04-27T06:45:48","date_gmt":"2015-04-27T06:45:48","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=863"},"modified":"2015-04-27T06:56:43","modified_gmt":"2015-04-27T06:56:43","slug":"how-to-check-presence-of-string-in-array-list","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-to-check-presence-of-string-in-array-list\/","title":{"rendered":"How to Check presence of String in JSON Array"},"content":{"rendered":"<p>While Using JSON Array for adding Details the required filter parameter in the Array may appear in any part of the Array.To Filter the Array which has many multiple data types i.e String, Integer, boolean you need to do <strong>instanceof comparison<\/strong><\/p>\n<p>In the below JSON array has a List of Employee Details from which Employee from a Particular location is taken out after Filtering<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\"> \r\n package com.mugil.json;\r\n\r\nimport java.util.Iterator;\r\n\r\nimport org.json.JSONArray;\r\nimport org.json.JSONException;\r\n\r\npublic class Employee {\r\n\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tString Location = &quot;Chennai&quot;;\r\n\t\tJSONArray arrEmpDetails = getEmpList();\r\n\r\n\t\tSystem.out\r\n\t\t\t\t.println(&quot;*-----------------Before Filter-----------------------------*&quot;);\r\n\t\tdisplayEmpDetails(arrEmpDetails);\r\n\t\tarrEmpDetails = filterEmpByLocation(arrEmpDetails, Location);\r\n\r\n\t\tSystem.out\r\n\t\t\t\t.println(&quot;*-----------------After Filter-----------------------------*&quot;);\r\n\t\tdisplayEmpDetails(arrEmpDetails);\r\n\t\tSystem.out\r\n\t\t\t\t.println(&quot;*-----------------------------------------------------*&quot;);\r\n\r\n\t}\r\n\r\n\tprivate static void displayEmpDetails(JSONArray arrEmpDetails) {\r\n\t\tJSONArray arrEmpDet = new JSONArray();\r\n\r\n\t\tfor (int i = 0; i &lt; arrEmpDetails.length(); i++) {\r\n\t\t\ttry {\r\n\t\t\t\tarrEmpDet = arrEmpDetails.getJSONArray(i);\r\n\t\t\t\tSystem.out.println(arrEmpDet);\r\n\t\t\t} catch (JSONException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t}\r\n\r\n\tprivate static JSONArray filterEmpByLocation(JSONArray arrEmpDetails,\r\n\t\t\tString location) {\r\n\t\tJSONArray arrEmpByLoc = new JSONArray();\r\n\t\tString strToComp = null;\r\n\t\tboolean isSameLoc = false;\r\n\r\n\t\tfor (int i = 0; i &lt; arrEmpDetails.length(); i++) {\r\n\t\t\ttry {\r\n\t\t\t\tJSONArray type = arrEmpDetails.getJSONArray(i);\r\n\r\n\t\t\t\tfor (int j = 0; j &lt; type.length(); j++) {\r\n\t\t\t\t\tObject strString = type.get(j);\r\n\t\t\t\t\tif (strString instanceof java.lang.String) {\r\n\t\t\t\t\t\tstrToComp = (String) strString;\r\n\t\t\t\t\t\tisSameLoc = strToComp.equals(location);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif (isSameLoc) {\r\n\t\t\t\t\tarrEmpByLoc.put(type);\r\n\t\t\t\t\tisSameLoc = false;\r\n\t\t\t\t}\r\n\r\n\t\t\t} catch (JSONException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn arrEmpByLoc;\r\n\t}\r\n\r\n\tpublic static JSONArray getEmpList() {\r\n\t\tJSONArray arrEmpDetails = new JSONArray();\r\n\t\tJSONArray arrPerDetails = new JSONArray();\r\n\r\n\t\tarrPerDetails.put(&quot;Empl1&quot;);\r\n\t\tarrPerDetails.put(23);\r\n\t\tarrPerDetails.put(&quot;Address1&quot;);\r\n\t\tarrPerDetails.put(&quot;Chennai&quot;);\r\n\t\tarrEmpDetails.put(arrPerDetails);\r\n\r\n\t\tarrPerDetails = new JSONArray();\r\n\t\tarrPerDetails.put(&quot;Empl2&quot;);\r\n\t\tarrPerDetails.put(23);\r\n\t\tarrPerDetails.put(&quot;Address2&quot;);\r\n\t\tarrPerDetails.put(&quot;Coimbatore&quot;);\r\n\t\tarrEmpDetails.put(arrPerDetails);\r\n\r\n\t\tarrPerDetails = new JSONArray();\r\n\t\tarrPerDetails.put(&quot;Empl3&quot;);\r\n\t\tarrPerDetails.put(24);\r\n\t\tarrPerDetails.put(&quot;Address3&quot;);\r\n\t\tarrPerDetails.put(&quot;Tirchy&quot;);\r\n\t\tarrEmpDetails.put(arrPerDetails);\r\n\r\n\t\tarrPerDetails = new JSONArray();\r\n\t\tarrPerDetails.put(&quot;Empl4&quot;);\r\n\t\tarrPerDetails.put(26);\r\n\t\tarrPerDetails.put(&quot;Address4&quot;);\r\n\t\tarrPerDetails.put(&quot;Chennai&quot;);\r\n\t\tarrEmpDetails.put(arrPerDetails);\r\n\r\n\t\treturn arrEmpDetails;\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While Using JSON Array for adding Details the required filter parameter in the Array may appear in any part of the Array.To Filter the Array which has many multiple data types i.e String, Integer, boolean you need to do instanceof comparison In the below JSON array has a List of Employee Details from which Employee&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-to-check-presence-of-string-in-array-list\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[],"class_list":["post-863","post","type-post","status-publish","format-standard","hentry","category-json"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=863"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions"}],"predecessor-version":[{"id":865,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/863\/revisions\/865"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}