日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android怎么根据标题解析json,如何在android中解析没有json对象标题的json数组?

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android怎么根据标题解析json,如何在android中解析没有json对象标题的json数组? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,我創建了解析器類JSONParser.java

package com.example.myparse;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.StatusLine;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

static InputStream is = null;

static JSONArray jarray = null;

static String json = "";

// constructor

public JSONParser() {

}

public JSONArray getJSONFromUrl(String url) {

StringBuilder builder = new StringBuilder();

HttpClient client = new DefaultHttpClient();

HttpGet httpGet = new HttpGet(url);

try {

HttpResponse response = client.execute(httpGet);

StatusLine statusLine = response.getStatusLine();

int statusCode = statusLine.getStatusCode();

if (statusCode == 200) {

HttpEntity entity = response.getEntity();

InputStream content = entity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(content));

String line;

while ((line = reader.readLine()) != null) {

builder.append(line);

}

} else {

Log.e("==>", "Failed to download file");

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// try parse the string to a JSON object

try {

jarray = new JSONArray( builder.toString());

//System.out.println(""+jarray);

} catch (JSONException e) {

Log.e("JSON Parser", "Error parsing data " + e.toString());

}

// return JSON String

return jarray;

}

}然后像這樣在主類中創建一個對象....

ArrayList> contactList = new ArrayList>(); // used to display in list view

JSONParser jParser = new JSONParser();

JSONArray json = jParser.getJSONFromUrl(url); // pass your ulr here

for(int i = 0; i <= json.length(); i++) // using for loop for parsing

{

try

{

JSONObject c = json.getJSONObject(i);

String name = c.getString(TAG_NAME);

String imagename = c.getString(TAG_IMAGENAME);

String active = c.getString(TAG_ACTIVE);

String createdon = c.getString(TAG_CREATEDON);

String lastmodifiedon = c.getString(TAG_LASTMODIFIEDON);

String description = c.getString(TAG_DESCRIPTION);

String id = c.getString(TAG_ID);

// If you want to show your parsed value in list view add the values into the array list

// creating new HashMap

HashMap map = new HashMap();

// adding each child node to HashMap key => value

map.put(TAG_NAME, name);

map.put(TAG_IMAGENAME, imagename);

map.put(TAG_ACTIVE, active);

map.put(TAG_CREATEDON, createdon);

map.put(TAG_LASTMODIFIEDON, lastmodifiedon);

map.put(TAG_DESCRIPTION, description);

map.put(TAG_ID, id);

// adding HashList to ArrayList

contactList.add(map);

System.out.println("contactlist---->"+contactList);

}

catch (JSONException e)

{

e.printStackTrace();

}

}我是這樣做的。我得到了正確的輸出。祝一切順利。

總結

以上是生活随笔為你收集整理的android怎么根据标题解析json,如何在android中解析没有json对象标题的json数组?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。