bbe*_*ard 176
注意:这个答案自2013年以来一直没有更新.建议的下载json的方法不再推荐用于android了,因为从api 23开始已经从sdk中删除了http客户端.
但是,下面的解析逻辑仍然适用
Android拥有解析内置json所需的所有工具.示例如下,不需要GSON或类似的东西.
获取您的JSON:
String result = "{\"someKey\":\"someValue\"}";
Run Code Online (Sandbox Code Playgroud)
现在你有了JSON,那又怎样?
创建一个JSONObject:
JSONObject jObject = new JSONObject(result);
Run Code Online (Sandbox Code Playgroud)
获取特定字符串
String result = "[{\"someKey\":\"someValue\"}]"
Run Code Online (Sandbox Code Playgroud)
获取特定的布尔值
String aJsonString = jObject.getString("STRINGNAME");
Run Code Online (Sandbox Code Playgroud)
获取特定的整数
boolean aJsonBoolean = jObject.getBoolean("BOOLEANNAME");
Run Code Online (Sandbox Code Playgroud)
得到一个特定的长
int aJsonInteger = jObject.getInt("INTEGERNAME");
Run Code Online (Sandbox Code Playgroud)
获得特定的双倍
long aJsonLong = jObject.getLong("LONGNAME");
Run Code Online (Sandbox Code Playgroud)
要获取特定的JSONArray:
double aJsonDouble = jObject.getDouble("DOUBLENAME");
Run Code Online (Sandbox Code Playgroud)
从阵列中获取项目
JSONArray jArray = jObject.getJSONArray("ARRAYNAME");
Run Code Online (Sandbox Code Playgroud)
小智 18
编写JSON解析器类
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Run Code Online (Sandbox Code Playgroud)解析JSON数据一旦你创建了解析器类,下一步就是知道如何使用该类.下面我将解释如何使用解析器类解析json(在此示例中使用).
2.1.存储在变量所有这些节点名称:在接触JSON我们有像姓名,电子邮件地址,性别和电话号码的项目.首先,将所有这些节点名称存储在变量中.打开主活动类并声明将所有节点名称存储在静态变量中.
// url to make request
private static String url = "http://api.9android.net/contacts";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
// contacts JSONArray
JSONArray contacts = null;
Run Code Online (Sandbox Code Playgroud)
2.2.使用解析器类获取JSONObject并循环遍历每个json项.下面我创建一个JSONParser类的实例并使用for循环我循环遍历每个json项目,最后将每个json数据存储在变量中.
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
}
} catch (JSONException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
Ljd*_*son 12
我为你编写了一个简单的例子并注明了源代码.该示例显示了如何抓取实时json并解析为JSONObject以进行详细信息提取:
try{
// Create a new HTTP Client
DefaultHttpClient defaultClient = new DefaultHttpClient();
// Setup the get request
HttpGet httpGetRequest = new HttpGet("http://example.json");
// Execute the request in the client
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
// Grab the response
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(json);
} catch(Exception e){
// In your production code handle any errors and catch the individual exceptions
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
获得JSONObject后,请参阅SDK以获取有关如何提取所需数据的详细信息.
| 归档时间: |
|
| 查看次数: |
481235 次 |
| 最近记录: |