android json解析及简单例子
jopen
13年前
<p></p> <h1><span style="font-family:SimHei;font-size:24px;">JSON的定义:</span></h1> <p></p> <p style="font-size:16px;"> <span style="font-family:KaiTi_GB2312;">一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为。 – Json.org</span></p> <p><strong><span style="line-height:26px;font-family:Arial;color:#333333;"><span style="font-size:16px;">JSON Vs XML</span></span><br style="text-align:left;line-height:26px;font-family:Arial;color:#333333;font-size:14px;" /> </strong></p> <blockquote style="border-bottom:medium none;border-left:medium none;padding-bottom:0px;margin:0px 0px 0px 40px;padding-left:0px;padding-right:0px;border-top:medium none;border-right:medium none;padding-top:0px;"> <p><span style="font-size:16px;"><span style="text-align:left;line-height:26px;color:#333333;"><span style="font-family:KaiTi_GB2312;">1.JSON和XML的数据可读性基本相同</span></span></span></p> </blockquote> <blockquote style="border-bottom:medium none;border-left:medium none;padding-bottom:0px;margin:0px 0px 0px 40px;padding-left:0px;padding-right:0px;border-top:medium none;border-right:medium none;padding-top:0px;"> <p><span style="font-size:16px;"><span style="text-align:left;line-height:26px;color:#333333;"><span style="font-family:KaiTi_GB2312;">2.JSON和XML同样拥有丰富的解析手段</span></span></span></p> <p><span style="font-size:16px;"><span style="text-align:left;line-height:26px;color:#333333;"><span style="font-family:KaiTi_GB2312;">3.JSON相对于XML来讲,数据的体积小</span></span></span></p> <p><span style="font-size:16px;"><span style="text-align:left;line-height:26px;color:#333333;"><span style="font-family:KaiTi_GB2312;">4.JSON与JavaScript的交互更加方便</span></span></span></p> <p><span style="font-size:16px;"><span style="font-family:KaiTi_GB2312;">5.JSON对数据的描述性比XML较差</span></span></p> <p></p> <div> <span style="color:#333333;font-size:16px;"><span style="line-height:26px;"><span style="font-family:KaiTi_GB2312;">6.JSON的速度要远远快于XML</span></span></span> </div> </blockquote> <strong><br /> </strong> <p></p> <p><strong><span style="font-size:16px;">android2.3提供的json解析类</span></strong><span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;"> </span></p> <span style="font-size:16px;"><span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;">android的json解析部分都在包org.json下,主要有以下几个类: </span><br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;" /> </span> <blockquote style="border-bottom:medium none;border-left:medium none;padding-bottom:0px;margin:0px 0px 0px 40px;padding-left:0px;padding-right:0px;border-top:medium none;border-right:medium none;padding-top:0px;"> <p><span style="font-size:16px;"><span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;"><strong>JSONObject</strong>:可以看作是一个json对象,<span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">这是系统中有关</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">JSON</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">定义的基本单元,其包含一对儿</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">(Key/Value)</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">数值。它对外部</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">(External</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">: 应用</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">toString()</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">方法输出的数值</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">)</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">调用的响应体现为一个标准的字符串(例如:</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;color:#76923c;"><span style="font-family:Calibri;">{"JSON": "Hello, World"}</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">,最外被大括号包裹,其中的</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">Key</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">和</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">Value</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">被冒号</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">":"</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">分隔)。其对于内部</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">(Internal)</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">行为的操作格式略微,例如:初始化一个</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">JSONObject</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">实例,引用内部的</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">put()</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">方法添加数值:</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;color:#76923c;"><span style="font-family:Calibri;">new JSONObject().put("JSON", "Hello, World!")</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">,在</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">Key</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">和</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">Value</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">之间是以逗号</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;" lang="EN-US"><span style="font-family:Calibri;">","</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;">分隔。</span></span></span><span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;" lang="EN-US"><span style="font-family:Calibri;">Value</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">的类型包括:</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;" lang="EN-US"><span style="font-family:Calibri;">Boolean</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">、</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;" lang="EN-US"><span style="font-family:Calibri;">JSONArray</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">、</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;" lang="EN-US"><span style="font-family:Calibri;">JSONObject</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">、</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;" lang="EN-US"><span style="font-family:Calibri;">Number</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">、</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;" lang="EN-US"><span style="font-family:Calibri;">String</span></span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">或者默认值</span><span style="text-align:left;line-height:25px;text-indent:28px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;" lang="EN-US"><span style="font-family:Calibri;">JSONObject.NULL object 。</span></span></p> </blockquote> <blockquote style="border-bottom:medium none;border-left:medium none;padding-bottom:0px;margin:0px 0px 0px 40px;padding-left:0px;padding-right:0px;border-top:medium none;border-right:medium none;padding-top:0px;"> <p><span style="font-size:16px;"><span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;"><strong>JSONStringer</strong>:json文本构建类 ,根据官方的解释,<span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">这个类可以帮助快速和便捷的创建JSON text<span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">。其最大的优点在于可以减少由于 格式的错误导致程序异常,引用这个类可以自动严格按照JSON语法规则(syntax rules)创建JSON text。每个JSONStringer实体只能对应创建一个JSON text。</span><span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">。其最大的优点在于可以减少由于格式的错误导致程序异常,引用这个类可以自动严格按照JSON语法规则(syntax rules)创建JSON text。每个JSONStringer实体只能对应创建一个JSON text。</span></span></span></span></p> </blockquote> <blockquote style="border-bottom:medium none;border-left:medium none;padding-bottom:0px;margin:0px 0px 0px 40px;padding-left:0px;padding-right:0px;border-top:medium none;border-right:medium none;padding-top:0px;"> <p style="text-align:left;"><span style="font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;"><span style="line-height:25px;"><strong>JSONArray</strong>:</span></span><span style="line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">它代表一组有序的数值。将其转换为String输出(toString)所表现的形式是用方括号包裹,数值以逗号”,”分隔(例如: [value1,value2,value3],大家可以亲自利用简短的代码更加直观的了解其格式)。这个类的内部同样具有查询行为, get()和opt()两种方法都可以通过index索引返回指定的数值,put()方法用来添加或者替换数值。</span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:16px;">同样这个类的value类型可以包括:Boolean、JSONArray、JSONObject、Number、String或者默认值JSONObject.NULL object。</span></p> </blockquote> <blockquote style="border-bottom:medium none;border-left:medium none;padding-bottom:0px;margin:0px 0px 0px 40px;padding-left:0px;padding-right:0px;border-top:medium none;border-right:medium none;padding-top:0px;"> <span style="font-size:16px;"><span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;"><strong>JSONTokener</strong>:json解析类 <br /> </span></span> <span style="font-size:16px;"><span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;"><strong>JSONException</strong>:json中用到的异常 </span></span> </blockquote> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <strong><span style="font-size:16px;">JSONObject, JSONArray来构建json文本</span></strong> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;"> </span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <div style="text-align:left;padding-bottom:1px;line-height:25px;overflow-x:auto;overflow-y:auto;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;font-weight:bold;padding-top:3px;" class="tools"> 代码 <a style="color:#108ac6;" title="收藏这段代码" href="/misc/goto?guid=4959500743487817234"></a> </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;margin:0px 0px 1px;padding-left:0px;padding-right:0px;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// 假设现在要创建这样一个json文本</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// {</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "phone" : ["12345678", "87654321"], // 数组</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "name" : "yuanzhifei89", // 字符串</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "age" : 100, // 数值</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "address" : { "country" : "china", "province" : "jiangsu" }, // 对象</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "married" : false // 布尔值</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// }</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">try</span> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 首先最外层是{},是创建一个对象</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> JSONObject person = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span> JSONObject(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 第一个键phone的值是数组,所以需要创建数组对象</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> JSONArray phone = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span> JSONArray(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> phone.put(<span style="color:blue;" class="string">"12345678"</span>).put(<span style="color:blue;" class="string">"87654321"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> person.put(<span style="color:blue;" class="string">"phone"</span>, phone); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> person.put(<span style="color:blue;" class="string">"name"</span>, <span style="color:blue;" class="string">"yuanzhifei89"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> person.put(<span style="color:blue;" class="string">"age"</span>, <span style="color:#c00000;" class="number">100</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 键address的值是对象,所以又要创建一个对象</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> JSONObject address = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span> JSONObject(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> address.put(<span style="color:blue;" class="string">"country"</span>, <span style="color:blue;" class="string">"china"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> address.put(<span style="color:blue;" class="string">"province"</span>, <span style="color:blue;" class="string">"jiangsu"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> person.put(<span style="color:blue;" class="string">"address"</span>, address); </span><span style="color:#000000;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> person.put(<span style="color:blue;" class="string">"married"</span>, <span style="color:#7f0055;font-weight:bold;" class="keyword">false</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;">} <span style="color:#7f0055;font-weight:bold;" class="keyword">catch</span> (JSONException ex) { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 键为null或使用json不支持的数字格式(NaN, infinities)</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">throw</span> <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span> RuntimeException(ex); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;">} </span></li> </ol> </div> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <strong><span style="font-size:16px;">getType和optType api的使用</span></strong> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;"> </span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <blockquote style="border-bottom:medium none;border-left:medium none;padding-bottom:0px;margin:0px 0px 0px 40px;padding-left:0px;padding-right:0px;border-top:medium none;border-right:medium none;padding-top:0px;"> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;">getType可以将要获取的键的值转换为指定的类型,如果无法转换或没有值则抛出JSONException <br /> </span> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;">optType也是将要获取的键的值转换为指定的类型,无法转换或没有值时返回用户提供或这默认提供的值 </span> </blockquote> <div style="text-align:left;padding-bottom:1px;line-height:25px;overflow-x:auto;overflow-y:auto;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;font-weight:bold;padding-top:3px;" class="tools"> 代码 <a style="color:#108ac6;" title="收藏这段代码" href="/misc/goto?guid=4959500743487817234"></a> </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;margin:0px 0px 1px;padding-left:0px;padding-right:0px;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">try</span> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 所有使用的对象都是用上面创建的对象</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 将第一个电话号码转换为数值和将名字转换为数值</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> phone.getLong(<span style="color:#c00000;" class="number">0</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> person.getLong(<span style="color:blue;" class="string">"name"</span>); <span style="color:#008200;" class="comment">// 会抛异常,因为名字无法转换为long</span> </span><span style="color:#000000;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> phone.optLong(<span style="color:#c00000;" class="number">0</span>); <span style="color:#008200;" class="comment">// 代码内置的默认值</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> phone.optLong(<span style="color:#c00000;" class="number">0</span>, <span style="color:#c00000;" class="number">1000</span>); <span style="color:#008200;" class="comment">// 用户提供的默认值</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> person.optLong(<span style="color:blue;" class="string">"name"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> person.optLong(<span style="color:blue;" class="string">"name"</span>, <span style="color:#c00000;" class="number">1000</span>); <span style="color:#008200;" class="comment">// 不像上面那样抛异常,而是返回1000</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;">} <span style="color:#7f0055;font-weight:bold;" class="keyword">catch</span> (JSONException ex) { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 异常处理代码</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;">} </span></li> </ol> </div> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <strong><span style="font-size:16px;">除了上面的两个类,还可以使用JSONStringer来构建json文本</span></strong> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;"> </span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <div style="text-align:left;padding-bottom:1px;line-height:25px;overflow-x:auto;overflow-y:auto;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;font-weight:bold;padding-top:3px;" class="tools"> Java代码 <a style="color:#108ac6;" title="收藏这段代码" href="/misc/goto?guid=4959500743487817234"></a> </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;margin:0px 0px 1px;padding-left:0px;padding-right:0px;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">try</span> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> JSONStringer jsonText = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span> JSONStringer(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 首先是{,对象开始。object和endObject必须配对使用</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.object(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:#000000;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.key(<span style="color:blue;" class="string">"phone"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 键phone的值是数组。array和endArray必须配对使用</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.array(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.value(<span style="color:blue;" class="string">"12345678"</span>).value(<span style="color:blue;" class="string">"87654321"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.endArray(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.key(<span style="color:blue;" class="string">"name"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.value(<span style="color:blue;" class="string">"yuanzhifei89"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.key(<span style="color:blue;" class="string">"age"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.value(<span style="color:#c00000;" class="number">100</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.key(<span style="color:blue;" class="string">"address"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 键address的值是对象</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.object(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.key(<span style="color:blue;" class="string">"country"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.value(<span style="color:blue;" class="string">"china"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.key(<span style="color:blue;" class="string">"province"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.value(<span style="color:blue;" class="string">"jiangsu"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.endObject(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.key(<span style="color:blue;" class="string">"married"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.value(<span style="color:#7f0055;font-weight:bold;" class="keyword">false</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// },对象结束</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonText.endObject(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;">} <span style="color:#7f0055;font-weight:bold;" class="keyword">catch</span> (JSONException ex) { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#7f0055;font-weight:bold;" class="keyword">throw</span> <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span> RuntimeException(ex); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;color:#2b91af;font-size:1em;padding-top:0px;"><span style="color:black;">} </span></li> </ol> </div> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <strong><span style="font-size:16px;">json文本解析类JSONTokener</span></strong> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;"> </span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;">按照RFC4627规范将json文本解析为相应的对象。 </span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <strong><span style="font-size:16px;">对于将json文本解析为对象,只需要用到该类的两个api:</span></strong> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;"> </span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;">构造函数 </span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;">public Object nextValue(); </span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <div style="text-align:left;padding-bottom:1px;line-height:25px;overflow-x:auto;overflow-y:auto;padding-left:1px;width:694px;padding-right:1px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;word-wrap:break-word;margin-left:9px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div class="bar"> <div style="padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;font-weight:bold;padding-top:3px;" class="tools"> 代码 <a style="color:#108ac6;" title="收藏这段代码" href="/misc/goto?guid=4959500743487817234"></a> </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;margin:0px 0px 1px;padding-left:0px;padding-right:0px;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// {</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "phone" : ["12345678", "87654321"], // 数组</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "name" : "yuanzhifei89", // 字符串</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "age" : 100, // 数值</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "address" : { "country" : "china", "province" : "jiangsu" }, // 对象</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// "married" : false // 布尔值</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#008200;" class="comment">// }</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">private</span> <span style="color:#7f0055;font-weight:bold;" class="keyword">static</span> <span style="color:#7f0055;font-weight:bold;" class="keyword">final</span> String JSON = </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:blue;" class="string">"{"</span> + </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:blue;" class="string">" \"phone\" : [\"12345678\", \"87654321\"],"</span> + </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:blue;" class="string">" \"name\" : \"yuanzhifei89\","</span> + </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:blue;" class="string">" \"age\" : 100,"</span> + </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:blue;" class="string">" \"address\" : { \"country\" : \"china\", \"province\" : \"jiangsu\" },"</span> + </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:blue;" class="string">" \"married\" : false,"</span> + </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:blue;" class="string">"}"</span>; </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">try</span> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> JSONTokener jsonParser = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span> JSONTokener(JSON); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 此时还未读取任何json文本,直接读取就是一个JSONObject对象。</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 如果此时的读取位置在"name" : 了,那么nextValue就是"yuanzhifei89"(String)</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> JSONObject person = (JSONObject) jsonParser.nextValue(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 接下来的就是JSON对象的操作了</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> person.getJSONArray(<span style="color:blue;" class="string">"phone"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> person.getString(<span style="color:blue;" class="string">"name"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> person.getInt(<span style="color:blue;" class="string">"age"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> person.getJSONObject(<span style="color:blue;" class="string">"address"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> person.getBoolean(<span style="color:blue;" class="string">"married"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">} <span style="color:#7f0055;font-weight:bold;" class="keyword">catch</span> (JSONException ex) { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 异常处理代码</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">} </span></li> </ol> </div> <p></p> <p><span style="font-size:16px;"><strong>其它的api基本就是用来查看json文本中的文本的</strong><span style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;"> </span></span></p> <div style="text-align:left;padding-bottom:1px;overflow-x:auto;overflow-y:auto;padding-left:1px;width:694px;padding-right:1px;word-wrap:break-word;margin-left:9px;word-break:break-all;padding-top:1px;" class="dp-highlighter"> <div style="line-height:25px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;" class="bar"> <div style="padding-bottom:3px;margin:0px;padding-left:3px;padding-right:3px;font-weight:bold;padding-top:3px;" class="tools"> 代码 <a style="color:#108ac6;" title="收藏这段代码" href="/misc/goto?guid=4959500743487817234"></a> </div> </div> <ol style="border-bottom:#d1d7dc 1px solid;border-left:#d1d7dc 1px solid;padding-bottom:2px;line-height:1.4em;margin:0px 0px 1px;padding-left:0px;padding-right:0px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;color:#2b91af;font-size:1em;border-top:#d1d7dc 1px solid;border-right:#d1d7dc 1px solid;padding-top:2px;" class="dp-j"> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"><span style="color:#7f0055;font-weight:bold;" class="keyword">try</span> { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> JSONTokener jsonParser = <span style="color:#7f0055;font-weight:bold;" class="keyword">new</span> JSONTokener(JSON); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 继续向下读8个json文本中的字符。此时刚开始,即在{处</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.next(<span style="color:#c00000;" class="number">8</span>); <span style="color:#008200;" class="comment">//{ "phone。tab算一个字符</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 继续向下读1个json文本中的字符</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.next(); <span style="color:#008200;" class="comment">//"</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 继续向下读取一个json文本中的字符。该字符不是空白、同时也不是注视中的字符</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.nextClean(); <span style="color:#008200;" class="comment">//:</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 返回当前的读取位置到第一次遇到'a'之间的字符串(不包括a)。</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.nextString(<span style="color:blue;" class="string">'a'</span>); <span style="color:#008200;" class="comment">// ["12345678", "87654321"], "n(前面有两个空格)</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 返回当前读取位置到第一次遇到字符串中(如"0089")任意字符之间的字符串,同时该字符是trimmed的。(此处就是第一次遇到了89)</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.nextTo(<span style="color:blue;" class="string">"0089"</span>); <span style="color:#008200;" class="comment">//me" : "yuanzhifei</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 读取位置撤销一个</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.back(); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.next(); <span style="color:#008200;" class="comment">//i</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 读取位置前进到指定字符串处(包括字符串)</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.skipPast(<span style="color:blue;" class="string">"address"</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.next(<span style="color:#c00000;" class="number">8</span>); <span style="color:#008200;" class="comment">//" : { "c</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 读取位置前进到执行字符处(不包括字符)</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.skipTo(<span style="color:blue;" class="string">'m'</span>); </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> jsonParser.next(<span style="color:#c00000;" class="number">8</span>); <span style="color:#008200;" class="comment">//married"</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">} <span style="color:#7f0055;font-weight:bold;" class="keyword">catch</span> (JSONException ex) { </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;"> <span style="color:#008200;" class="comment">// 异常处理代码</span> </span></li> <li style="border-left:#d1d7dc 1px solid;padding-bottom:0px;line-height:18px;background-color:#fafafa;margin:0px 0px 0px 38px;padding-left:10px;padding-right:0px;font-size:1em;padding-top:0px;"><span style="color:black;">} </span></li> </ol> </div> <p><span style="font-size:16px;"><span style="line-height:18px;font-family:Monaco,'DejaVu Sans Mono','Bitstream Vera Sans Mono',Consolas,'Courier New',monospace;font-size:16px;"> 以下是一个标准的JSON请求实现过程:</span><br /> </span></p> <p></p> <pre class="brush:java; toolbar: true; auto-links: false;">HttpPost request = new HttpPost(url); // 先封装一个 JSON 对象 JSONObject param = new JSONObject(); param.put("name", "rarnu"); param.put("password", "123456"); // 绑定到请求 Entry StringEntity se = new StringEntity(param.toString()); request.setEntity(se); // 发送请求 HttpResponse httpResponse = new DefaultHttpClient().execute(request); // 得到应答的字符串,这也是一个 JSON 格式保存的数据 String retSrc = EntityUtils.toString(httpResponse.getEntity()); // 生成 JSON 对象 JSONObject result = new JSONObject( retSrc); String token = result.get("token"); </pre> <br /> <span style="font-size:16px;"> 下面这个是自己修改别人的小例子,主要是加一些注释和讲解,这个例子主要是使用android进行json解析。</span> <br style="text-align:left;line-height:25px;font-family:Helvetica,Tahoma,Arial,sans-serif;font-size:14px;" /> <pre class="brush:xml; toolbar: true; auto-links: false;">单数据{'singer':{'id':01,'name':'tom','gender':'男'}} 多个数据{"singers":[ {'id':02,'name':'tom','gender':'男'}, {'id':03,'name':'jerry,'gender':'男'}, {'id':04,'name':'jim,'gender':'男'}, {'id':05,'name':'lily,'gender':'女'}]} </pre> <span style="text-align:left;font-size:16px;"> 下面的类主要是解析单个数据parseJson()和多个数据的方法parseJsonMulti():</span> <br style="text-align:left;" /> <pre class="brush:java; toolbar: true; auto-links: false;">public class JsonActivity extends Activity { /** Called when the activity is first created. */ private TextView tvJson; private Button btnJson; private Button btnJsonMulti; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvJson = (TextView) this.findViewById(R.id.tvJson); btnJson = (Button) this.findViewById(R.id.btnJson); btnJsonMulti = (Button) this.findViewById(R.id.btnJsonMulti); btnJson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // url // String strUrl = "http://10.158.166.110:8080/AndroidServer/JsonServlet"; String strUrl = ServerPageUtil.getStrUrl(UrlsOfServer.JSON_SINGER); //获得返回的Json字符串 String strResult = connServerForResult(strUrl); //解析Json字符串 parseJson(strResult); } }); btnJsonMulti.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String strUrl = ServerPageUtil.getStrUrl(UrlsOfServer.JSON_SINGERS); String strResult = connServerForResult(strUrl); //获得多个Singer parseJsonMulti(strResult); } }); } private String connServerForResult(String strUrl) { // HttpGet对象 HttpGet httpRequest = new HttpGet(strUrl); String strResult = ""; try { // HttpClient对象 HttpClient httpClient = new DefaultHttpClient(); // 获得HttpResponse对象 HttpResponse httpResponse = httpClient.execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 取得返回的数据 strResult = EntityUtils.toString(httpResponse.getEntity()); } } catch (ClientProtocolException e) { tvJson.setText("protocol error"); e.printStackTrace(); } catch (IOException e) { tvJson.setText("IO error"); e.printStackTrace(); } return strResult; } // 普通Json数据解析 private void parseJson(String strResult) { try { JSONObject jsonObj = new JSONObject(strResult).getJSONObject("singer"); int id = jsonObj.getInt("id"); String name = jsonObj.getString("name"); String gender = jsonObj.getString("gender"); tvJson.setText("ID号"+id + ", 姓名:" + name + ",性别:" + gender); } catch (JSONException e) { System.out.println("Json parse error"); e.printStackTrace(); } } //解析多个数据的Json private void parseJsonMulti(String strResult) { try { JSONArray jsonObjs = new JSONObject(strResult).getJSONArray("singers"); String s = ""; for(int i = 0; i < jsonObjs.length() ; i++){ JSONObject jsonObj = ((JSONObject)jsonObjs.opt(i)) .getJSONObject("singer"); int id = jsonObj.getInt("id"); String name = jsonObj.getString("name"); String gender = jsonObj.getString("gender"); s += "ID号"+id + ", 姓名:" + name + ",性别:" + gender+ "\n" ; } tvJson.setText(s); } catch (JSONException e) { System.out.println("Jsons parse error !"); e.printStackTrace(); } } } </pre>