Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JSON数据解析到listView中 如何将JSON数据放入listview?我所要做的就是遍历JSON数据,获取前两个元素,并将它们添加到listview中的两个文本字段中_Java_Android_Arrays_Json_Listview - Fatal编程技术网

Java 将JSON数据解析到listView中 如何将JSON数据放入listview?我所要做的就是遍历JSON数据,获取前两个元素,并将它们添加到listview中的两个文本字段中

Java 将JSON数据解析到listView中 如何将JSON数据放入listview?我所要做的就是遍历JSON数据,获取前两个元素,并将它们添加到listview中的两个文本字段中,java,android,arrays,json,listview,Java,Android,Arrays,Json,Listview,但当我编写下面的代码时,它只是将整个数组元素放入列表视图和括号中。它只增加主数组而不是子项。(如果有道理的话) 下面是我的json数据,魔法发生在“//循环数组””之后: 这是我目前的脚本: import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class ShowShop extends ListActivity { private static fi

但当我编写下面的代码时,它只是将整个数组元素放入列表视图和括号中。它只增加主数组而不是子项。(如果有道理的话)

下面是我的json数据,魔法发生在“//循环数组””之后:

这是我目前的脚本:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ShowShop extends ListActivity {

    private static final String TAG = "MyApp";  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ziplist_layout);

         Bundle bundle = getIntent().getExtras();
         String shop_data = bundle.getString("shopData");
         Log.v(TAG, shop_data);

         ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        //Get the data (see above)
               try{
            //Get the element that holds the earthquakes ( JSONArray )
                   JSONArray jsonShopArray = new JSONArray(shop_data);


                        **//Loop the Array**
                for(int i=0;i < jsonArray.length();i++){                        

HashMap<String, String> map = new HashMap<String, String>();
                JSONArray e = jsonShopArray.getJSONArray(i);

                Log.v(TAG, e.toString(i));
                map.put("id",  String.valueOf(1));
                map.put("name", "Store name:" + e.toString(2));
                map.put("zipcode", "Zipcode: " +  e.toString(3));
                mylist.add(map);
        }
               }catch(JSONException e)        {
                 Log.e("log_tag", "Error parsing data "+e.toString());
               }

               ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.shop_list,
                       new String[] { "name", "zipcode" },
                       new int[] { R.id.item_title, R.id.item_subtitle });

       setListAdapter(adapter);




        /**Toast.makeText(ShowShop.this, zipReturn, Toast.LENGTH_LONG).show(); */

    }
}
import org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类ShowShop扩展了ListActivity{
私有静态最终字符串TAG=“MyApp”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ziplist_布局);
Bundle Bundle=getIntent().getExtras();
String shop_data=bundle.getString(“shopData”);
Log.v(标签、车间数据);
ArrayList mylist=新的ArrayList();
//获取数据(见上文)
试一试{
//获取保存地震的元素(JSONArray)
JSONArray jsonShopArray=新的JSONArray(车间数据);
**//循环数组**
对于(inti=0;i
e.toString
不正确)


您应该使用
getString
getInt
或getwhich

e.toString
不正确)


您应该使用
getString
getInt
或getwhich

在哪里定义
map
变量?很抱歉,我在处理它时遗漏了它。更新。
jsonShopArray
实际上是
jsonArray
?:)您是否使用jackson或gson之类的库?更新以显示库您是正确的Nikita。您在哪里定义
map
变量?很抱歉,我在处理此问题时遗漏了它。更新。
jsonShopArray
实际上是
jsonArray
?:)你使用过像jackson或gson这样的库吗?更新以显示你是正确的库Nikita。明白了,这是我的问题。谢谢+1这是我的问题。谢谢+1
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ShowShop extends ListActivity {

    private static final String TAG = "MyApp";  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ziplist_layout);

         Bundle bundle = getIntent().getExtras();
         String shop_data = bundle.getString("shopData");
         Log.v(TAG, shop_data);

         ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        //Get the data (see above)
               try{
            //Get the element that holds the earthquakes ( JSONArray )
                   JSONArray jsonShopArray = new JSONArray(shop_data);


                        **//Loop the Array**
                for(int i=0;i < jsonArray.length();i++){                        

HashMap<String, String> map = new HashMap<String, String>();
                JSONArray e = jsonShopArray.getJSONArray(i);

                Log.v(TAG, e.toString(i));
                map.put("id",  String.valueOf(1));
                map.put("name", "Store name:" + e.toString(2));
                map.put("zipcode", "Zipcode: " +  e.toString(3));
                mylist.add(map);
        }
               }catch(JSONException e)        {
                 Log.e("log_tag", "Error parsing data "+e.toString());
               }

               ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.shop_list,
                       new String[] { "name", "zipcode" },
                       new int[] { R.id.item_title, R.id.item_subtitle });

       setListAdapter(adapter);




        /**Toast.makeText(ShowShop.this, zipReturn, Toast.LENGTH_LONG).show(); */

    }
}