当前位置:   article > 正文

java 数组 放入 list_java – 将JSON数据放入List中

java遍历json添加进list

我从我的服务器正确地获取了JSON数据,我只想将它放入以下数组中,但我不确定JSON数据是否正确插入到ArrayList中.

这是阵列

private List createList(int size) {

List result = new ArrayList();

for (int i = 1; i <= size; i++) {

ShopInfo ci = new ShopInfo();

ci.name = TAG_NAME+i;

ci.address = TAG_ADDRESS+i;

result.add(ci);

}

return result;

}

我的json

{"success":1,"shops":[{"name":"Test_Shop","address":"1 Big Road Dublin"}

文件

public class OrderCoffee extends Activity {

JSONParser jParser = new JSONParser();

ArrayList> shopList;

private static String url_all_products = "xxxxxxxxxx/ordercoffee.php";

// products JSONArray

JSONArray shops = null;

// JSON Node names

private static final String TAG_SUCCESS = "success";

private static final String TAG_SHOPS = "shops";

private static final String TAG_NAME = "name";

private static final String TAG_ADDRESS = "address";

//get a list of participating coffee shops in the locality that are using the app

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.order_coffee);

new LoadAllProducts().execute();

RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);

recList.setHasFixedSize(true);

LinearLayoutManager llm = new LinearLayoutManager(this);

llm.setOrientation(LinearLayoutManager.VERTICAL);

recList.setLayoutManager(llm);

shopList = new ArrayList>();

ShopAdapter ca = new ShopAdapter(createList(3));

recList.setAdapter(ca);

}

class LoadAllProducts extends AsyncTask {

@Override

protected String doInBackground(String... args) {

// Building Parameters

List params = new ArrayList();

// getting JSON string from URL

JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

// Check your log cat for JSON reponse

Log.d("All Products: ", json.toString());

try {

// Checking for SUCCESS TAG

int success = json.getInt(TAG_SUCCESS);

if (success == 1) {

// products found

// Getting Array

shops = json.getJSONArray(TAG_SHOPS);

// looping through All Products

for (int i = 0; i < shops.length(); i++) {

JSONObject c = shops.getJSONObject(i);

// Storing each json item in variable

String id = c.getString(TAG_ADDRESS);

String name = c.getString(TAG_NAME);

// creating new HashMap

HashMap map = new HashMap();

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

map.put(TAG_ADDRESS, id);

map.put(TAG_NAME, name);

shopList.add(map);

}

} else {

}

} catch (JSONException e) {

e.printStackTrace();

}

return null;

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

private List createList(int size) {

List result = new ArrayList();

for (int i = 1; i <= size; i++) {

ShopInfo ci = new ShopInfo();

ci.name = TAG_NAME+i;

ci.address = TAG_ADDRESS+i;

result.add(ci);

}

return result;

}

}

ShopAdapter

package com.example.strobe.coffeetime;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import java.util.List;

/**

* Created by root on 10/04/15.

*/

public class ShopAdapter extends RecyclerView.Adapter {

private List shopList;

public ShopAdapter(List shopList) {

this.shopList = shopList;

}

@Override

public int getItemCount() {

return shopList.size();

}

@Override

public void onBindViewHolder(ShopViewHolder shopViewHolder, int i) {

ShopInfo ci = shopList.get(i);

shopViewHolder.vName.setText(ci.name);

shopViewHolder.vAddress.setText(ci.address);

}

@Override

public ShopViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

View itemView = LayoutInflater.

from(viewGroup.getContext()).

inflate(R.layout.card_layout, viewGroup, false);

return new ShopViewHolder(itemView);

}

public static class ShopViewHolder extends RecyclerView.ViewHolder {

protected TextView vName;

protected TextView vAddress;

public ShopViewHolder(View v) {

super(v);

vName = (TextView) v.findViewById(R.id.name);

vAddress = (TextView) v.findViewById(R.id.address);

}

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/278716
推荐阅读
相关标签
  

闽ICP备14008679号