猿问

如何解析Android中的JSON?

如何解析Android中的JSON?

米琪卡哇伊
浏览 570回答 3
3回答

白猪掌柜的

Android拥有解析内置json所需的所有工具。示例如下,不需要GSON或类似的东西。获取您的JSON:DefaultHttpClient&nbsp;&nbsp;&nbsp;httpclient&nbsp;=&nbsp;new&nbsp;DefaultHttpClient(new&nbsp;BasicHttpParams());HttpPost&nbsp;httppost&nbsp;=&nbsp;new&nbsp;HttpPost (http://someJSONUrl/jsonWebService);//&nbsp;Depends&nbsp;on&nbsp;your&nbsp;web&nbsp;servicehttppost.setHeader("Content-type",&nbsp;"application/json"); InputStream&nbsp;inputStream&nbsp;=&nbsp;null;String&nbsp;result&nbsp;=&nbsp;null;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;HttpResponse&nbsp;response&nbsp;=&nbsp;httpclient.execute(httppost);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;HttpEntity&nbsp;entity&nbsp;=&nbsp;response.getEntity(); &nbsp;&nbsp;&nbsp;&nbsp;inputStream&nbsp;=&nbsp;entity.getContent(); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;json&nbsp;is&nbsp;UTF-8&nbsp;by&nbsp;default &nbsp;&nbsp;&nbsp;&nbsp;BufferedReader&nbsp;reader&nbsp;=&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(inputStream,&nbsp;"UTF-8"),&nbsp;8); &nbsp;&nbsp;&nbsp;&nbsp;StringBuilder&nbsp;sb&nbsp;=&nbsp;new&nbsp;StringBuilder(); &nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;line&nbsp;=&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((line&nbsp;=&nbsp;reader.readLine())&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append(line&nbsp;+&nbsp;"\n"); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;sb.toString();}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Oops}finally&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;try{if(inputStream&nbsp;!=&nbsp;null)inputStream.close();}catch(Exception&nbsp;squish){}}现在你有了JSON,那又怎样?创建一个JSONObject:JSONObject&nbsp;jObject&nbsp;=&nbsp;new&nbsp;JSONObject(result);获取特定字符串String&nbsp;aJsonString&nbsp;=&nbsp;jObject.getString("STRINGNAME");获取特定的布尔值boolean&nbsp;aJsonBoolean&nbsp;=&nbsp;jObject.getBoolean("BOOLEANNAME");获取特定的整数int&nbsp;aJsonInteger&nbsp;=&nbsp;jObject.getInt("INTEGERNAME");得到一个特定的长long&nbsp;aJsonLong&nbsp;=&nbsp;jObject.getLong("LONGNAME");获得特定的双倍double&nbsp;aJsonDouble&nbsp;=&nbsp;jObject.getDouble("DOUBLENAME");要获取特定的JSONArray:JSONArray&nbsp;jArray&nbsp;=&nbsp;jObject.getJSONArray("ARRAYNAME");从阵列中获取项目for&nbsp;(int&nbsp;i=0;&nbsp;i&nbsp;<&nbsp;jArray.length();&nbsp;i++){ &nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JSONObject&nbsp;oneObject&nbsp;=&nbsp;jArray.getJSONObject(i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Pulling&nbsp;items&nbsp;from&nbsp;the&nbsp;array &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;oneObjectsItem&nbsp;=&nbsp;oneObject.getString("STRINGNAMEinTHEarray"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;oneObjectsItem2&nbsp;=&nbsp;oneObject.getString("anotherSTRINGNAMEINtheARRAY"); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(JSONException&nbsp;e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Oops &nbsp;&nbsp;&nbsp;&nbsp;}}
随时随地看视频慕课网APP

相关分类

Android
Java
我要回答