Java 如何将JSON结果加载到listview中

Java 如何将JSON结果加载到listview中,java,android,json,Java,Android,Json,我是Android和Java新手。有谁能告诉我如何将http请求得到的json结果从URL加载到ListView。在下面的onpostExecute中,我得到了结果,但不知道如何将其加载到listview中 protected void onPostExecute(String result) { try { JSONObject json = new JSONObject(result); e

我是Android和Java新手。有谁能告诉我如何将http请求得到的json结果从URL加载到ListView。在下面的onpostExecute中,我得到了结果,但不知道如何将其加载到listview中

       protected void onPostExecute(String result) {
            try {
                JSONObject json = new JSONObject(result);
                etResponse.setText(json.getString("name"));

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
       }
这是我的列表视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <!-- Main ListView 
         Always give id value as list(@android:id/list)
    -->
    <ListView
        android:id="@+id/test_list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

For an example my Json looks like this:
{
id: "1",
name: "Any name",
email: "Any Email"
}

例如,我的Json如下所示:
{
id:“1”,
姓名:“任何姓名”,
电子邮件:“任何电子邮件”
}

谢谢。

您转换为字符串的json数据必须与博客文章不同,因此您可以将其转换为jsonArray,并在数组上循环,从数组中获取每个json对象,并将值分配给listview。您需要实际的代码还是逻辑

String jsonres = '{
id: "1",
name: "Any name",
email: "Any Email"
}';
您可以执行以下操作:

ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
JSONObject myJsonObject = new JSONObject(jsonres);
ListView mylv = (ListView) findViewById(R.id.mylvID);
HashMap<String, String> item = new HashMap<String, String>();
Sring id = myJsonObject.getString("id");
String name = myJsonObject.getString("name");
String email = myJsonObject.getString("email");

item.put("id", id);
item.put("name", name);
item.put("email", email);

items.add(item);
String[] keys = {"id", "name", "email"};//our keys for which we take the corresponding value
int[] ids = {R.id.title, R.id.author, R.id.date};

SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.list_view_item, keys, ids);
mylv.setAdapter(adapter);
ArrayList items=new ArrayList();
JSONObject myJsonObject=新的JSONObject(jsonres);
ListView mylv=(ListView)findViewById(R.id.mylvID);
HashMap项=新建HashMap();
sringid=myJsonObject.getString(“id”);
String name=myJsonObject.getString(“name”);
String email=myJsonObject.getString(“电子邮件”);
项目.投入(“id”,id);
项目。填写(“名称”,名称);
项目。放置(“电子邮件”,电子邮件);
项目。添加(项目);
字符串[]键={“id”,“name”,“email”}//为其获取相应值的键
int[]id={R.id.title,R.id.author,R.id.date};
SimpleAdapter=新SimpleAdapter(此,项,R.layout.list\u视图\项,键,ID);
mylv.setAdapter(适配器);
//list_view_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainListActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:weightSum="1"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp">

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.85"
            android:gravity="left">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/blogPostImage"
                android:src="@drawable/ic_launcher"/>
        </TableLayout>

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.2"
            android:gravity="right">

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Large Text is a very large and long text"
                    android:id="@+id/title" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:weightSum="1">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="Author's Name"
                        android:id="@+id/author"
                        android:layout_weight="0.7"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:text="2014-15-16 14:15:16"
                        android:id="@+id/date"
                        android:layout_weight="0.3"
                        android:gravity="right"/>
                </LinearLayout>
            </TableRow>
        </TableLayout>

    </LinearLayout>
</RelativeLayout>


我还建议您阅读更多关于listadapters的内容,因为这将帮助您更好地理解您正在做的事情。

您的JSON是什么样子的,我们无法猜测您是否知道。您还没有显示任何相关的代码我这里有一个教程:我尝试通过jsonArray循环并将每个数据添加到ArrayList,然后将其加载到适配器,但这似乎在Asynctask上不起作用。您是否尝试在doInBackground方法中执行此操作。您不能在其中执行此操作,因为它在单独的线程上运行,因此您可以在onPostExecute中执行此操作。我建议创建一个全局JsonObject变量,并在doInBackground中以JsonObject的形式返回数据。然后您可以创建一个方法来转换为数组并填充listview。感谢@gurinderhans的快速响应,我已经将json结果加载到arraylist中。但问题是如何将arraylist加载到适配器。再次更新答案请再次检查:)你必须重命名内容。谢谢兄弟:)这不是我想要的,但我有主意了。