<?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>jackson &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/jackson/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 03 May 2022 05:18:13 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] JAVA Jackson解析无名(匿名)数组</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-jackson%e8%a7%a3%e6%9e%90%e6%97%a0%e5%90%8d%e5%8c%bf%e5%90%8d%e6%95%b0%e7%bb%84/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-jackson%e8%a7%a3%e6%9e%90%e6%97%a0%e5%90%8d%e5%8c%bf%e5%90%8d%e6%95%b0%e7%bb%84/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 03 May 2022 05:18:13 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[jackson]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[匿名]]></category>
		<category><![CDATA[无名]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13557</guid>

					<description><![CDATA[<p>假设有JSON文件 1.json：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="json language-json hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">[
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;sku&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;ABC&#34;</span>,
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;num&#34;</span>:&#160;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">3</span>
&#160;&#160;&#160;&#160;},
&#160;&#160;&#160;&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;sku&#34;</span>:&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;DEF&#34;</span>,
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&#34;num&#34;</span>:&#160;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">2</span>
&#160;&#160;&#160;&#160;}
]
</code></pre>
</section>
<p>由中括号可知，这个JSON字符串映射成JAVA对象是一个List，而它里面又包含了两个JAVA对象（每个大括号对应一个）。<br />
<span id="more-13557"></span><br />
假设JSON字符串&#160;<span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;">{ </span><span style="font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&#34;sku&#34;</span><span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre; color: rgb(163, 21, 21);">&#34;ABC&#34;</span><span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;">, </span><span style="font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&#34;num&#34;</span><span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre; color: rgb(9, 134, 88);">3</span><span style="background-color: rgb(255, 255, 255); font-family: &#34;JetBrains Mono&#34;, Menlo, Monaco, &#34;Courier New&#34;, monospace; font-size: 15px; white-space: pre;"> }</span>&#160;映射成的POJO为：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&#160;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">SkuInfo</span>&#160;</span>{
&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&#160;String&#160;sku;
&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>&#160;num;
}
</code></pre>
</section>
<p>那么，应该怎么用Jackson库来解析原JSON？<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-jackson%e8%a7%a3%e6%9e%90%e6%97%a0%e5%90%8d%e5%8c%bf%e5%90%8d%e6%95%b0%e7%bb%84/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>假设有JSON文件 1.json：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="json language-json hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">[
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;sku&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ABC&quot;</span>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;num&quot;</span>:&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">3</span>
&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;sku&quot;</span>:&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;DEF&quot;</span>,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">&quot;num&quot;</span>:&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">2</span>
&nbsp;&nbsp;&nbsp;&nbsp;}
]
</code></pre>
</section>
<p>由中括号可知，这个JSON字符串映射成JAVA对象是一个List，而它里面又包含了两个JAVA对象（每个大括号对应一个）。<br />
<span id="more-13557"></span><br />
假设JSON字符串&nbsp;<span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">{ </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&quot;sku&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(163, 21, 21);">&quot;ABC&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">, </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&quot;num&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(9, 134, 88);">3</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;"> }</span>&nbsp;映射成的POJO为：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">SkuInfo</span>&nbsp;</span>{
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;String&nbsp;sku;
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;num;
}
</code></pre>
</section>
<p>那么，应该怎么用Jackson库来解析原JSON？<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
由于JSON字符串&nbsp;<span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">{ </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&quot;sku&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(163, 21, 21);">&quot;ABC&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">, </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(4, 81, 165);">&quot;num&quot;</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;">: </span><span style="font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre; color: rgb(9, 134, 88);">3</span><span style="background-color: rgb(255, 255, 255); font-family: &quot;JetBrains Mono&quot;, Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 15px; white-space: pre;"> }</span>&nbsp;是无名（匿名）的，所以要用这样的方法来解析它：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">final</span>&nbsp;ObjectMapper&nbsp;mapper&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;ObjectMapper();
SkuInfo[]&nbsp;skuInfos&nbsp;=&nbsp;mapper.readValue(FileUtils.readFileToString(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;1.json&quot;</span>),&nbsp;Charsets.UTF_8),&nbsp;SkuInfo[].class);
</code></pre>
</section>
<p>或者你不想把JSON解析成一个JAVA数组，而是一个List的话，可以这么做：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">List&lt;SkuInfo&gt;&nbsp;skuInfos&nbsp;=&nbsp;MAPPER.readValue(FileUtils.readFileToString(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;1.json&quot;</span>),&nbsp;Charsets.UTF_8),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;TypeReference&lt;List&lt;SkuInfo&gt;&gt;()&nbsp;{});
</code></pre>
</section>
<p>
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：<br />
<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><br />
以及我的微信视频号：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java-jackson%e8%a7%a3%e6%9e%90%e6%97%a0%e5%90%8d%e5%8c%bf%e5%90%8d%e6%95%b0%e7%bb%84/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 如何用Jackson解析含有转义字符的JSON到Java对象？</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e7%94%a8jackson%e8%a7%a3%e6%9e%90%e5%90%ab%e6%9c%89%e8%bd%ac%e4%b9%89%e5%ad%97%e7%ac%a6%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%b0java%e5%af%b9%e8%b1%a1/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e7%94%a8jackson%e8%a7%a3%e6%9e%90%e5%90%ab%e6%9c%89%e8%bd%ac%e4%b9%89%e5%ad%97%e7%ac%a6%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%b0java%e5%af%b9%e8%b1%a1/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 22 Feb 2019 05:17:50 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[escaped]]></category>
		<category><![CDATA[jackson]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[斜杠]]></category>
		<category><![CDATA[解析]]></category>
		<category><![CDATA[转义字符]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10256</guid>

					<description><![CDATA[<p>
如果一个JSON字符串里有一个字段，它的值又是一个完整的JSON字符串，那么这时候，它可能会含有转义字符。举个例子，我们有一个文件&#160; codelast.json，它的内容为一行字符串：</p>
<blockquote>
<p>
		{&#34;aaa&#34;:&#34;{\&#34;ccc\&#34;:\&#34;ccc\&#34;,\&#34;ddd\&#34;:\&#34;ddd\&#34;}&#34;,&#34;bbb&#34;:{&#34;ccc&#34;:&#34;ccc&#34;,&#34;ddd&#34;:&#34;ddd&#34;}}</p>
</blockquote>
<p>现在要用Jackson解析它为一个Java对象，怎么做？<br />
<span id="more-10256"></span><br />
我们先把这个JSON格式化，便于观察它的结构：</p>
<pre class="CodeContainer" style="margin-top: 0px; margin-bottom: 0px; padding: 8.5px; font-family: Menlo, Monaco, Consolas, &#34;Courier New&#34;, monospace; font-size: 12.025px; color: rgb(51, 51, 51); border-radius: 4px; line-height: 18px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border-style: solid; border-color: rgba(0, 0, 0, 0.15);">
<span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">{</span>
    <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&#34;aaa&#34;</span>: <span class="String" style="color: rgb(0, 119, 119);">&#34;{\&#34;ccc\&#34;:\&#34;ccc\&#34;,\&#34;ddd\&#34;:\&#34;ddd\&#34;}&#34;<span class="Comma" style="color: rgb(0, 0, 0); font-weight: bold;">,</span> </span>
    <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&#34;bbb&#34;</span>: <span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">{</span>
        <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&#34;ccc&#34;</span>: <span class="String" style="color: rgb(0, 119, 119);">&#34;ccc&#34;<span class="Comma" style="color: rgb(0, 0, 0); font-weight: bold;">,</span> </span>
        <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&#34;ddd&#34;</span>: <span class="String" style="color: rgb(0, 119, 119);">&#34;ddd&#34;</span>
    <span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">}</span>
<span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">}</span></pre>
<p>所以按照Jackson的套路，我们似乎很容易就可以写出对应的JSON解析代码，先定义几个POJO：<br />
A.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>A {
  <span style="color:#cc7832;">private </span>AAA <span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  private </span>BBB <span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>AAA <span style="color:#ffc66d;">getAaa</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setAaa</span>(AAA aaa) {
    <span style="color:#cc7832;">this</span>.</pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e7%94%a8jackson%e8%a7%a3%e6%9e%90%e5%90%ab%e6%9c%89%e8%bd%ac%e4%b9%89%e5%ad%97%e7%ac%a6%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%b0java%e5%af%b9%e8%b1%a1/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
如果一个JSON字符串里有一个字段，它的值又是一个完整的JSON字符串，那么这时候，它可能会含有转义字符。举个例子，我们有一个文件&nbsp; codelast.json，它的内容为一行字符串：</p>
<blockquote>
<p>
		{&quot;aaa&quot;:&quot;{\&quot;ccc\&quot;:\&quot;ccc\&quot;,\&quot;ddd\&quot;:\&quot;ddd\&quot;}&quot;,&quot;bbb&quot;:{&quot;ccc&quot;:&quot;ccc&quot;,&quot;ddd&quot;:&quot;ddd&quot;}}</p>
</blockquote>
<p>现在要用Jackson解析它为一个Java对象，怎么做？<br />
<span id="more-10256"></span><br />
我们先把这个JSON格式化，便于观察它的结构：</p>
<pre class="CodeContainer" style="margin-top: 0px; margin-bottom: 0px; padding: 8.5px; font-family: Menlo, Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12.025px; color: rgb(51, 51, 51); border-radius: 4px; line-height: 18px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border-style: solid; border-color: rgba(0, 0, 0, 0.15);">
<span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">{</span>
    <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&quot;aaa&quot;</span>: <span class="String" style="color: rgb(0, 119, 119);">&quot;{\&quot;ccc\&quot;:\&quot;ccc\&quot;,\&quot;ddd\&quot;:\&quot;ddd\&quot;}&quot;<span class="Comma" style="color: rgb(0, 0, 0); font-weight: bold;">,</span> </span>
    <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&quot;bbb&quot;</span>: <span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">{</span>
        <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&quot;ccc&quot;</span>: <span class="String" style="color: rgb(0, 119, 119);">&quot;ccc&quot;<span class="Comma" style="color: rgb(0, 0, 0); font-weight: bold;">,</span> </span>
        <span class="PropertyName" style="color: rgb(204, 0, 0); font-weight: bold;">&quot;ddd&quot;</span>: <span class="String" style="color: rgb(0, 119, 119);">&quot;ddd&quot;</span>
    <span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">}</span>
<span class="ObjectBrace" style="color: rgb(0, 170, 0); font-weight: bold;">}</span></pre>
<p>所以按照Jackson的套路，我们似乎很容易就可以写出对应的JSON解析代码，先定义几个POJO：<br />
A.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>A {
  <span style="color:#cc7832;">private </span>AAA <span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  private </span>BBB <span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>AAA <span style="color:#ffc66d;">getAaa</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setAaa</span>(AAA aaa) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">aaa </span>= aaa<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public </span>BBB <span style="color:#ffc66d;">getBbb</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setBbb</span>(BBB bbb) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">bbb </span>= bbb<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
AAA.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>AAA {
  <span style="color:#cc7832;">private </span>String <span style="color:#9876aa;">ccc</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  private </span>String <span style="color:#9876aa;">ddd</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>String <span style="color:#ffc66d;">getCcc</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">ccc</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setCcc</span>(String ccc) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">ccc </span>= ccc<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public </span>String <span style="color:#ffc66d;">getDdd</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">ddd</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setDdd</span>(String ddd) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">ddd </span>= ddd<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}
</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
BBB.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>BBB {
  <span style="color:#cc7832;">private </span>String <span style="color:#9876aa;">ccc</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  private </span>String <span style="color:#9876aa;">ddd</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>String <span style="color:#ffc66d;">getCcc</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">ccc</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setCcc</span>(String ccc) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">ccc </span>= ccc<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public </span>String <span style="color:#ffc66d;">getDdd</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">ddd</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setDdd</span>(String ddd) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">ddd </span>= ddd<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
然后就是解析的逻辑：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
String json = FileUtils.<span style="font-style:italic;">readFileToString</span>(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/codelast.json&quot;</span>))<span style="color:#cc7832;">;
</span>ObjectMapper mapper = <span style="color:#cc7832;">new </span>ObjectMapper()<span style="color:#cc7832;">;
</span>A data = mapper.readValue(json<span style="color:#cc7832;">, </span>A.<span style="color:#cc7832;">class</span>)<span style="color:#cc7832;">;</span></pre>
<p>我们的目的是把codelast.json这个文件里保存的JSON字符串解析成类A的一个对象data。</p>
<p>这不是很简单嘛？要不运行一下试试看？<br />
马上报错：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="hljs coffeescript" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">com.fasterxml.jackson.databind.exc.MismatchedInputException:&nbsp;Cannot&nbsp;construct&nbsp;instance&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">of</span>&nbsp;`<span class="javascript" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">com.codelast.AAA</span>`&nbsp;(although&nbsp;at&nbsp;least&nbsp;one&nbsp;Creator&nbsp;exists):&nbsp;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">no</span>&nbsp;String-argument&nbsp;constructor/factory&nbsp;method&nbsp;to&nbsp;deserialize&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">from</span>&nbsp;String&nbsp;value&nbsp;(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;{&quot;ccc&quot;:&quot;ccc&quot;,&quot;ddd&quot;:&quot;ddd&quot;}&#39;</span>)
&nbsp;at&nbsp;[Source:&nbsp;(String)<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;{&quot;</span>aaa<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;:&quot;</span>{\<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ccc\&quot;:\&quot;ccc\&quot;,\&quot;ddd\&quot;:\&quot;ddd\&quot;}&quot;</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;bbb&quot;</span>:{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ccc&quot;</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ccc&quot;</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ddd&quot;</span>:<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;ddd&quot;</span>}}
<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;;&nbsp;line:&nbsp;1,&nbsp;column:&nbsp;8]&nbsp;(through&nbsp;reference&nbsp;chain:&nbsp;com.codelast.A[&quot;</span>aaa<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;])

at&nbsp;com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at&nbsp;com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
at&nbsp;com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031)
at&nbsp;com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:371)
at&nbsp;com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:323)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1366)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:171)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
at&nbsp;com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
at&nbsp;com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at&nbsp;com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at&nbsp;com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
</span></code></pre>
</section>
<p> 这是什么情况？事实上，这就是由于JSON的&ldquo;aaa&rdquo;这个字段里面包含转义字符造成的，如果你Google一下，可能会找到<a href="https://www.zhihu.com/question/21074655" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这个</span></a>知乎帖子，它告诉我们，要为Jackson的 ObjectMapper 设置一个属性为true：<span style="color:#0000ff;">Feature.ALLOW_UNQUOTED_CONTROL_CHARS</span><br />
所以我们可以试试：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
String json = FileUtils.<span style="font-style:italic;">readFileToString</span>(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/codelast.json&quot;</span>))<span style="color:#cc7832;">;
</span>ObjectMapper mapper = <span style="color:#cc7832;">new </span>ObjectMapper()<span style="color:#cc7832;">;
</span>mapper.configure(JsonParser.Feature.<span style="color:#9876aa;font-style:italic;">ALLOW_UNQUOTED_CONTROL_CHARS</span><span style="color:#cc7832;">, true</span>)<span style="color:#cc7832;">;
</span>A data = mapper.readValue(json<span style="color:#cc7832;">, </span>A.<span style="color:#cc7832;">class</span>)<span style="color:#cc7832;">;</span></pre>
<p>再执行代码，会发现报一样的错。<br />
所以上面的解决方案是<span style="color:#ff0000;">无效</span>的。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
正确解决这个问题的办法，是参考 Stack Overflow的<a href="https://stackoverflow.com/questions/36462971/json-to-java-object-deserialization-with-escaped-properties" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这个</span></a>帖子。里面说到，应该创建一个&nbsp;JsonDeserializer 来搞定这个问题。下面，我就把正确的代码依次贴上来：<br />
A.java 的主要代码（和之前不一样，这里使用了一个自定义的 <span style="color:#ff0000;">AAADeserializer</span>）：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>A {
  <span style="color:#bbb529;">@JsonDeserialize</span>(<span style="color:#d0d0ff;">using </span>= AAADeserializer.<span style="color:#cc7832;">class</span>)
  <span style="color:#cc7832;">private </span>AAA <span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;

</span><span style="color:#cc7832;">  private </span>BBB <span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  public </span>AAA <span style="color:#ffc66d;">getAaa</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">aaa</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setAaa</span>(AAA aaa) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">aaa </span>= aaa<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public </span>BBB <span style="color:#ffc66d;">getBbb</span>() {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;">bbb</span><span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}

  <span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">setBbb</span>(BBB bbb) {
    <span style="color:#cc7832;">this</span>.<span style="color:#9876aa;">bbb </span>= bbb<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}
</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
AAA.java、BBB.java 和之前完全一样，不重复贴代码上来。<br />
下面就是关键的 AAADeserializer.java 的主要代码：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
<span style="color:#cc7832;">public class </span>AAADeserializer <span style="color:#cc7832;">extends </span>JsonDeserializer&lt;AAA&gt; {
  <span style="color:#cc7832;">private static final </span>ObjectMapper <span style="color:#9876aa;font-style:italic;">mapper </span>= <span style="color:#cc7832;">new </span>ObjectMapper()<span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  </span><span style="color:#bbb529;">@Override
</span><span style="color:#bbb529;">  </span><span style="color:#cc7832;">public </span>AAA <span style="color:#ffc66d;">deserialize</span>(JsonParser jsonParser<span style="color:#cc7832;">, </span>DeserializationContext deserializationContext) <span style="color:#cc7832;">throws </span>IOException {
    <span style="color:#cc7832;">return </span><span style="color:#9876aa;font-style:italic;">mapper</span>.readValue(jsonParser.getText()<span style="color:#cc7832;">, </span>AAA.<span style="color:#cc7832;">class</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
然后就可以解析JSON了：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'DejaVu Sans Mono';font-size:12.0pt;">
String json = FileUtils.<span style="font-style:italic;">readFileToString</span>(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/codelast.json&quot;</span>))<span style="color:#cc7832;">;
</span>ObjectMapper mapper = <span style="color:#cc7832;">new </span>ObjectMapper()<span style="color:#cc7832;">;
</span>A data = mapper.readValue(json<span style="color:#cc7832;">, </span>A.<span style="color:#cc7832;">class</span>)<span style="color:#cc7832;">;</span></pre>
<p>
你这时候再运行一下这段代码，就会发现不再报错，JSON已经成功解析到对象 data 中，问题解决！</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e7%94%a8jackson%e8%a7%a3%e6%9e%90%e5%90%ab%e6%9c%89%e8%bd%ac%e4%b9%89%e5%ad%97%e7%ac%a6%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%88%b0java%e5%af%b9%e8%b1%a1/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
