<?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>json &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Wed, 15 Nov 2023 08:01:32 +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>[原创] Apache Pig解析JSON数据</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-apache-pig%e8%a7%a3%e6%9e%90json%e6%95%b0%e6%8d%ae/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-apache-pig%e8%a7%a3%e6%9e%90json%e6%95%b0%e6%8d%ae/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 04 Jan 2022 18:20:14 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[apache pig]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[Parse]]></category>
		<category><![CDATA[解析]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13406</guid>

					<description><![CDATA[<div style="text-align: center;">
	<a href="https://www.codelast.com/" rel="noopener" target="_blank"><img decoding="async" alt="JSON" src="//www.codelast.com/wp-content/uploads/2022/01/json_logo.png" style="width: 300px; height: 68px;" /></a></div>
<p>
查看更多Apache Pig的教程请点击<a href="https://www.codelast.com/?p=4550" rel="noopener" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>在大数据处理领域，JSON格式的数据非常常见，然而用Apache Pig读取JSON并正确取出其中的字段我觉得并不算方便（在某些情况下很容易写错），所以总结一下几个常见的JSON loader/UDF的用法。</p>
<p>假设有数据文件 1.txt，内容是一行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;">{<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;items&#34;</span>:[{<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;id&#34;</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;">&#34;111&#34;</span>,<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;name&#34;</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;">&#34;aaa&#34;</span>,<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;extra&#34;</span>:{<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;k&#34;</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;">&#34;ttt&#34;</span>,<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;v&#34;</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;">&#34;uuu&#34;</span>}},{<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;id&#34;</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;">&#34;222&#34;</span>,<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;name&#34;</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;">&#34;bbb&#34;</span>,<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;extra&#34;</span>:{<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;k&#34;</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;">&#34;rrr&#34;</span>,<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;v&#34;</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;">&#34;sss&#34;</span>}}]}
</code></pre>
</section>
<p><span id="more-13406"></span><br />
为了便于观看，这里把它格式化如下（注意，Pig是按行读取数据，这里格式化只是为了写这篇文章便于展示，实际存储在文件里仍然是一行）： </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;<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;items&#34;</span>:&#160;[
&#160;&#160;&#160;&#160;&#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;id&#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;111&#34;</span>,
&#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;name&#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;aaa&#34;</span>,
&#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;extra&#34;</span>:&#160;{
&#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;k&#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;ttt&#34;</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;v&#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;uuu&#34;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;},
&#160;&#160;&#160;&#160;&#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;id&#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;222&#34;</span>,
&#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;name&#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;bbb&#34;</span>,
&#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;extra&#34;</span>:&#160;{
&#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;k&#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;rrr&#34;</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;v&#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;sss&#34;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;]
}
</code></pre>
</section>
<p>
我们的目的是正确解析这个JSON。</p>
<p>➤&#160;来自于<a href="https://github.com/twitter/elephant-bird" rel="noopener" target="_blank">Elephant Bird</a>的 com.twitter.elephantbird.pig.piggybank.JsonStringToMap()&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-apache-pig%e8%a7%a3%e6%9e%90json%e6%95%b0%e6%8d%ae/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<div style="text-align: center;">
	<a href="https://www.codelast.com/" rel="noopener" target="_blank"><img decoding="async" alt="JSON" src="//www.codelast.com/wp-content/uploads/2022/01/json_logo.png" style="width: 300px; height: 68px;" /></a></div>
<p>
查看更多Apache Pig的教程请点击<a href="https://www.codelast.com/?p=4550" rel="noopener" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>在大数据处理领域，JSON格式的数据非常常见，然而用Apache Pig读取JSON并正确取出其中的字段我觉得并不算方便（在某些情况下很容易写错），所以总结一下几个常见的JSON loader/UDF的用法。</p>
<p>假设有数据文件 1.txt，内容是一行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;">{<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;items&quot;</span>:[{<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;id&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;111&quot;</span>,<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;name&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;aaa&quot;</span>,<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;extra&quot;</span>:{<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;k&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;ttt&quot;</span>,<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;v&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;uuu&quot;</span>}},{<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;id&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;222&quot;</span>,<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;name&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-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;extra&quot;</span>:{<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;k&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;rrr&quot;</span>,<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;v&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;sss&quot;</span>}}]}
</code></pre>
</section>
<p><span id="more-13406"></span><br />
为了便于观看，这里把它格式化如下（注意，Pig是按行读取数据，这里格式化只是为了写这篇文章便于展示，实际存储在文件里仍然是一行）： </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;<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;items&quot;</span>:&nbsp;[
&nbsp;&nbsp;&nbsp;&nbsp;&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;id&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;111&quot;</span>,
&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;name&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;aaa&quot;</span>,
&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;extra&quot;</span>:&nbsp;{
&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;k&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;ttt&quot;</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;v&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;uuu&quot;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},
&nbsp;&nbsp;&nbsp;&nbsp;&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;id&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;222&quot;</span>,
&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;name&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;bbb&quot;</span>,
&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;extra&quot;</span>:&nbsp;{
&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;k&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;rrr&quot;</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;v&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;sss&quot;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;]
}
</code></pre>
</section>
<p>
我们的目的是正确解析这个JSON。</p>
<p>➤&nbsp;来自于<a href="https://github.com/twitter/elephant-bird" rel="noopener" target="_blank">Elephant Bird</a>的 com.twitter.elephantbird.pig.piggybank.JsonStringToMap()<br />
其实它不是一个JSON loader而是一个UDF，它的作用是把JSON字符串转成一个map。由于JSON结构中都是key:value形式的数据，所以把JSON字符串转成map之后就可以取出指定字段的内容。<br />
这个UDF对于只有&ldquo;一层&rdquo;的JSON非常好用，例如（格式化后）长下面这个样子的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;<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;id&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;111&quot;</span>,
&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;name&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;aaa&quot;</span>
}
</code></pre>
</section>
<p>此时，可以像下面这样取到字段 id&nbsp;及 name 的值：</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;">DEFINE&nbsp;Json2Map&nbsp;com.twitter.elephantbird.pig.piggybank.JsonStringToMap();

A&nbsp;=&nbsp;LOAD&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;1.txt&#39;</span>&nbsp;AS&nbsp;(jsonStr:&nbsp;chararray);
B&nbsp;=&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">FOREACH&nbsp;A&nbsp;GENERATE&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;">Json2Map</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(jsonStr)</span>&nbsp;AS&nbsp;jsonObj</span>;
C&nbsp;=&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">FOREACH&nbsp;B&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;">GENERATE</span>&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(chararray)</span>&nbsp;jsonObj#&#39;id&#39;&nbsp;AS&nbsp;id,&nbsp;<span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(chararray)</span>&nbsp;jsonObj#&#39;name&#39;&nbsp;AS&nbsp;name</span>;
DUMP&nbsp;C;
</code></pre>
</section>
<p>在这里，JSON被转成了一个map对象，用 <span style="color:#ff0000;">#&#39;id&#39;</span>&nbsp;的方式可以取出名为&nbsp;id&nbsp;的key对应的value。<br />
可见这个UDF在某些场合下用起来很方便，但对本文一开始举例的那种嵌套有多层的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 />
➤ Pig内置的 JsonLoader<br />
对付本文开头的那种JSON，我们可以使用Pig内置的&nbsp;JsonLoader&nbsp;来加载数据，使用它唯一的坏处就是需要定义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="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;">A&nbsp;=&nbsp;LOAD&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;1.txt&#39;</span>&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">USING&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;">JsonLoader</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(<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;items:&nbsp;{(id:chararray,&nbsp;name:chararray,&nbsp;extra:(k:chararray,&nbsp;v:chararray))}&#39;</span>)</span></span>;
B&nbsp;=&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">FOREACH&nbsp;A&nbsp;GENERATE&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;">FLATTEN</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(items)</span></span>;
C&nbsp;=&nbsp;FOREACH&nbsp;B&nbsp;GENERATE&nbsp;id,&nbsp;name,&nbsp;FLATTEN(extra)&nbsp;AS&nbsp;(k,&nbsp;v);
D&nbsp;=&nbsp;FOREACH&nbsp;C&nbsp;GENERATE&nbsp;id,&nbsp;name,&nbsp;k,&nbsp;v;
DUMP&nbsp;D;
</code></pre>
</section>
<p>我们仔细看一下本文开头的JSON，它的 <span style="color:#ff0000;">items</span>&nbsp;是一个数组，对应到Pig里就是一个bag（bag里的每个元素是一个tuple），因此用JsonLoader定义JSON的结构时，<span style="color:#ff0000;">items</span>&nbsp;是 <span style="color:#0000ff;">items: {()}</span>&nbsp;这样一种结构。<br />
而JSON中的&nbsp;<span style="color:#ff0000;">extra</span>&nbsp;字段对应的并不是一个数组，所以它对应到Pig里就是一个tuple，因此在JsonLoader中定义结构时&nbsp;<span style="color:#ff0000;">extra</span>&nbsp;是&nbsp;<span style="color:#0000ff;">extra:(k:chararray, v:chararray)</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 />
注意：JsonLoader看似好用，然而这里<span style="background-color:#ffff00;">有一个巨坑</span>&mdash;&mdash;JsonLoader中定义的字段名和JSON字符串中的字段名并不对应，它只跟顺序相关！<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="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;">A&nbsp;=&nbsp;LOAD&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;1.txt&#39;</span>&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">USING&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;">JsonLoader</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(<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;items:&nbsp;{(id:chararray,&nbsp;invalidField:chararray,&nbsp;extra:(k:chararray,&nbsp;v:chararray))}&#39;</span>)</span></span>;
B&nbsp;=&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">FOREACH&nbsp;A&nbsp;GENERATE&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;">FLATTEN</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(items)</span></span>;
C&nbsp;=&nbsp;FOREACH&nbsp;B&nbsp;GENERATE&nbsp;id,&nbsp;invalidField,&nbsp;FLATTEN(extra)&nbsp;AS&nbsp;(k,&nbsp;v);
D&nbsp;=&nbsp;FOREACH&nbsp;C&nbsp;GENERATE&nbsp;id,&nbsp;invalidField,&nbsp;k,&nbsp;v;
DUMP&nbsp;D;
</code></pre>
</section>
<p>竟然能正确输出：</p>
<blockquote>
<div>
		(111,aaa,ttt,uuu)</div>
<div>
		(222,bbb,rrr,sss)</div>
</blockquote>
<p>在上面的代码中，<span style="color:#ff0000;">invalidField</span>&nbsp;是一个在JSON字符串中根本不存在的字段名，然而它却能被JsonLoader正确识别，那是因为JsonLoader是按字段的顺序来识别的，JSON字符串中的第2个字段 <span style="color:#0000ff;">name</span>，被JsonLoader映射到了&nbsp;<span style="color: rgb(255, 0, 0);">invalidField&nbsp;</span>这个字段上。<br />
也正是因为这个原因，如果你的JSON字符串的各个字段顺序是不恒定的，就不能用这种方法来读取！<br />
并且，在JsonLoader中定义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="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;">A&nbsp;=&nbsp;LOAD&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;1.txt&#39;</span>&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">USING&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;">JsonLoader</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(<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;items:&nbsp;{(name:chararray,&nbsp;id:chararray,&nbsp;extra:(k:chararray,&nbsp;v:chararray))}&#39;</span>)</span></span>;
</code></pre>
</section>
<p>你以为JsonLoader会自动把JSON字符串中的&nbsp;<span style="color:#ff0000;">id</span>&nbsp;字段映射到你定义的第二个字段（id:chararray）上吗？根本不会！它会把它映射到 name:chararray&nbsp;上，因为在JSON字符串中，id&nbsp;字段是排在第1位的字段，而在JsonLoader中，name:chararray&nbsp;也是排第1位的字段，因此它俩就映射上了。<br />
所以，如果你有JSON数据的控制权，我建议在生成JSON数据的时候，固定生成字段的顺序。例如，如果是使用 Jackson的ObjectMapper生成JSON，那么可以通过&nbsp;@JsonPropertyOrder&nbsp;标注来控制JSON字段的顺序。<br />
另外，如果你定义的结构中漏了一个字段，很可能会造成解析不成功，导致程序逻辑不能按设想的执行！</p>
<p>最后，这仅仅是一个嵌套层数非常少的JSON，如果层数非常多的话，会导致你代码很难写，这个时候还是用JAVA写Map-Reduce&nbsp;job去处理吧。<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 />
<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-apache-pig%e8%a7%a3%e6%9e%90json%e6%95%b0%e6%8d%ae/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>
