<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Tutorials | Student Projects</title>
	<atom:link href="https://studentprojects.in/category/software-development/java/java-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sat, 10 Dec 2022 05:07:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>Class cast exception [JSONArray cannot be cast to org.json.simple.JSONObject]</title>
		<link>https://studentprojects.in/software-development/java/java-tutorials/class-cast-exception-jsonarray-cannot-be-cast-to-org-json-simple-jsonobject/</link>
					<comments>https://studentprojects.in/software-development/java/java-tutorials/class-cast-exception-jsonarray-cannot-be-cast-to-org-json-simple-jsonobject/#respond</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Wed, 09 Nov 2022 09:00:08 +0000</pubDate>
				<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[JSONArray to ArrayList]]></category>
		<category><![CDATA[ClassCastException]]></category>
		<category><![CDATA[JSON]]></category>
		<guid isPermaLink="false">https://studentprojects.in/?p=9768</guid>

					<description><![CDATA[<p>If you have a JSONObject and you are trying to convert it to a list, it is not enough to simply cast it to ArrayList as shown here. This will throw a ClassCastException. This is because your JSON contains an array. So create a JSONArray and iterate over it.</p>
<p>The post <a href="https://studentprojects.in/software-development/java/java-tutorials/class-cast-exception-jsonarray-cannot-be-cast-to-org-json-simple-jsonobject/">Class cast exception [JSONArray cannot be cast to org.json.simple.JSONObject]</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>If you have a JSONObject and you are trying to convert it to a list, it is not enough to simply cast it to ArrayList as shown here.</p>



<pre class="wp-block-code"><code lang="java" class="language-java">ArrayList arrayList = (ArrayList) jsonObject.get(category);</code></pre>



<p>This will throw a ClassCastException.</p>



<pre class="wp-block-code"><code lang="bash" class="language-bash">java.lang.ClassCastException: class org.json.JSONArray cannot be cast to class java.util.ArrayList (org.json.JSONArray is in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader @23ab930d; java.util.ArrayList is in module java.base of loader 'bootstrap')</code></pre>



<p>This is because your JSON contains an array. So create a JSONArray and iterate over it. </p>



<pre class="wp-block-code"><code lang="java" class="language-java">JSONArray jsonArray = jsonObject.getJSONArray(category);

ArrayList&lt;Object> arrayList = new ArrayList&lt;Object>();
if (jsonArray != null) {
    for (int i=0; i&lt;jsonArray.length(); i++){
        arrayList.add(jsonArray.get(i));
        System.out.println("Adding to the list {}", jsonArray.get(i));
    }
}</code></pre><p>The post <a href="https://studentprojects.in/software-development/java/java-tutorials/class-cast-exception-jsonarray-cannot-be-cast-to-org-json-simple-jsonobject/">Class cast exception [JSONArray cannot be cast to org.json.simple.JSONObject]</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/java/java-tutorials/class-cast-exception-jsonarray-cannot-be-cast-to-org-json-simple-jsonobject/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to convert List &#8211; Long  to long  or vise versa?</title>
		<link>https://studentprojects.in/software-development/java/java-tutorials/convert-listlong-long-vise-versa/</link>
					<comments>https://studentprojects.in/software-development/java/java-tutorials/convert-listlong-long-vise-versa/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Thu, 12 Jan 2012 11:58:40 +0000</pubDate>
				<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Long[] to long[]]]></category>
		<category><![CDATA[List to long[]]]></category>
		<category><![CDATA[long[] to List]]></category>
		<category><![CDATA[Long to primitive long]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=2539</guid>

					<description><![CDATA[<p>There is no automatic conversion from array of primitive type to array of their boxed reference types or vise versa. You have to iterate for each data. Converting List&#60;Long&#62; to long[] List input = getSomeLongs(); // Assume some values long[] output = new long[input.size()]; int index = 0; for(Long val : input) { indexess[index] =</p>
<p>The post <a href="https://studentprojects.in/software-development/java/java-tutorials/convert-listlong-long-vise-versa/">How to convert List – Long  to long  or vise versa?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>There is no automatic conversion from array of primitive type to array of their boxed reference types or vise versa. You have to iterate for each data.</p>
<p><strong>Converting List&lt;Long&gt; to long[]</strong></p>
<pre lang="java" line="1">List input = getSomeLongs(); // Assume some values
long[] output = new long[input.size()];
int index = 0;
for(Long val : input) {
	indexess[index] =  val;
	index++;
} 
</pre>
<p><strong>Converting long[] to List&lt;Long&gt;</strong></p>
<pre lang="java" line="1">long[] input = getSomeLongs(); // Assume some values
List output = new ArrayList();
for(long val : input){
	lst.add(val);
}
</pre><p>The post <a href="https://studentprojects.in/software-development/java/java-tutorials/convert-listlong-long-vise-versa/">How to convert List – Long  to long  or vise versa?</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/java/java-tutorials/convert-listlong-long-vise-versa/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
