Java JSON对齐可以循环相同的项
我在设置json数据时遇到问题,它只设置了最后一个项目4次,无法设置其他项目尝试了太多方法无法找到它,请朋友们在这里帮助 这是我正在使用的JSON: 这是javaJava JSON对齐可以循环相同的项,java,android,json,Java,Android,Json,我在设置json数据时遇到问题,它只设置了最后一个项目4次,无法设置其他项目尝试了太多方法无法找到它,请朋友们在这里帮助 这是我正在使用的JSON: 这是java public class Secondlevel extends Activity { List<JSONParser> itemsdata = new ArrayList<JSONParser>(); String item, ids; ListView sec; Secon
public class Secondlevel extends Activity {
List<JSONParser> itemsdata = new ArrayList<JSONParser>();
String item, ids;
ListView sec;
Second_adapter secondAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlist);
sec = (ListView) findViewById(R.id.seondlst);
secondAdapter = new Second_adapter(Secondlevel.this, itemsdata);
sec.setAdapter(secondAdapter);
Intent hello = getIntent();
item = hello.getStringExtra("name");
new loaditems().execute();
}
public class loaditems extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
String url = "http://www.yell4food.com/json/data_standard_item_new.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("rname","standardtakeaway"));
params.add(new BasicNameValuePair("cname", item));
params.add(new BasicNameValuePair("cids", ids));
Calling calling = new Calling();
String jurl = calling.makeHttpRequest(url, "GET", params);
Log.d("dataurls", jurl);
try {
JSONArray array = new JSONArray(jurl);
for (int i = 0; i < array.length(); i++) {
JSONObject first = array.getJSONObject(i);
JSONParser parser = new JSONParser();
parser.setMenuname(first.getString("menu_name"));
JSONArray getitems = first.getJSONArray("items");
for (int j = 0; j < getitems.length(); j++) {
JSONObject sitems = getitems.getJSONObject(j);
parser.setIid(sitems.getInt("id"));
parser.setBaseName(sitems.getString("BaseName"));
parser.setItemdesc(sitems.getString("itemdesc"));
JSONArray subitems = sitems.getJSONArray("subitems");
for (int l = 0; l < subitems.length(); l++) {
JSONObject thrid = subitems.getJSONObject(l);
parser.setSid(thrid.getInt("id"));
parser.setSubItemdesc(thrid.getString("SubItemdesc"));
parser.setSubItemprice(thrid.getString("SubItemprice"));
}
itemsdata.add(parser);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), ""+item, Toast.LENGTH_SHORT).show();
secondAdapter.notifyDataSetChanged();
}
}
}
公共类二级扩展活动{
List itemsdata=new ArrayList();
字符串项,id;
ListView证券交易委员会;
第二个适配器第二个适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlist);
sec=(ListView)findViewById(R.id.seondlst);
secondAdapter=新的第二个适配器(Secondlevel.this,itemsdata);
第二节设置适配器(第二适配器);
Intent hello=getIntent();
item=hello.getStringExtra(“名称”);
新建loaditems().execute();
}
公共类loaditems扩展了异步任务{
@凌驾
受保护的字符串背景(字符串…字符串){
字符串url=”http://www.yell4food.com/json/data_standard_item_new.php";
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“rname”、“standardtakeaway”);
参数添加(新的BasicNameValuePair(“cname”,项目));
参数添加(新的BasicNameValuePair(“cids”,ID));
调用=新调用();
String jurl=calling.makeHttpRequest(url,“GET”,params);
Log.d(“数据URL”,jurl);
试一试{
JSONArray数组=新的JSONArray(jurl);
对于(int i=0;i
这是适配器:
package com.app.prominere.standardtakeout;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by prominere on 20-Oct-15.
*/
public class Second_adapter extends BaseAdapter {
Context context;
TextView basename,SubItemprice,itemdesc,SubItemdesc;
LayoutInflater inflater;
private List<JSONParser> items;
public Second_adapter(Context context, List<JSONParser> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view==null)
view = inflater.inflate(R.layout.item_clicked, viewGroup, false);
basename = (TextView) view.findViewById(R.id.basename);
SubItemprice = (TextView) view.findViewById(R.id.SubItemprice);
itemdesc = (TextView) view.findViewById(R.id.itemdesc);
SubItemdesc = (TextView) view.findViewById(R.id.SubItemdesc);
JSONParser setdata = items.get(i);
basename.setText(setdata.getBaseName());
itemdesc.setText(setdata.getItemdesc());
SubItemdesc.setText(setdata.getSubItemdesc());
SubItemprice.setText(setdata.getSubItemprice());
return view;
}
}
package com.app.prominere.standardtakeout;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.TextView;
导入java.util.List;
/**
*由prominere于2015年10月20日创建。
*/
公共类第二个适配器扩展了BaseAdapter{
语境;
TextView basename、SubItemprice、itemdesc、SubItemdesc;
充气机;
私人清单项目;
公共第二个\u适配器(上下文、列表项){
this.context=上下文;
这个项目=项目;
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共对象getItem(int i){
返回项目。获取(i);
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
如果(视图==null)
视图=充气机。充气(R.layout.item_单击,视图组,false);
basename=(TextView)view.findViewById(R.id.basename);
SubItemprice=(TextView)view.findViewById(R.id.SubItemprice);
itemdesc=(TextView)view.findViewById(R.id.itemdesc);
SubItemdesc=(TextView)view.findViewById(R.id.SubItemdesc);
JSONParser setdata=items.get(i);
basename.setText(setdata.getBaseName());
itemdesc.setText(setdata.getItemdesc());
SubItemdesc.setText(setdata.getSubItemdesc());
SubItemprice.setText(setdata.getSubItemprice());
返回视图;
}
}
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="BaseName"
android:id="@+id/basename"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SubItemdesc"
android:id="@+id/SubItemdesc"
android:layout_below="@+id/basename"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SubItemprice"
android:id="@+id/SubItemprice"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/basename"
android:layout_margin="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SubItemdesc"
android:id="@+id/itemdesc"
android:layout_below="@id/SubItemdesc"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/order"
android:layout_margin="10dp"
android:src="@drawable/order"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
http://i.stack.imgur.com/SzhfK.png
http://i.stack.imgur.com/SzhfK.png
MoveJSONParser=newjsonparser()代码>在第二个for循环内
您的代码如下所示:
JSONObject first = array.getJSONObject(i);
JSONArray getitems = first.getJSONArray("items");
for (int j = 0; j < getitems.length(); j++) {
JSONParser parser = new JSONParser();
parser.setMenuname(first.getString("menu_name"));
JSONObject sitems = getitems.getJSONObject(j);
parser.setIid(sitems.getInt("id"));
parser.setBaseName(sitems.getString("BaseName"));
parser.setItemdesc(sitems.getString("itemdesc"));
JSONArray subitems = sitems.getJSONArray("subitems");
for (int l = 0; l < subitems.length(); l++) {
JSONObject thrid = subitems.getJSONObject(l);
parser.setSid(thrid.getInt("id"));
parser.setSubItemdesc(thrid.getString("SubItemdesc"));
parser.setSubItemprice(thrid.getString("SubItemprice"));
}
itemsdata.add(parser);
}
JSONObject first=array.getJSONObject(i);
JSONArray getitems=first.getJSONArray(“items”);
对于(int j=0;jimport java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Response {
@SerializedName("menu_name")
private String menuName;
@SerializedName("items")
private ArrayList<Items> items;
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public ArrayList<Items> getItems() {
return items;
}
public void setItems(ArrayList<Items> items) {
this.items = items;
}
}
import java.util.ArrayList;
import com.google.gson.annotations.SerializedName;
public class Items {
@SerializedName("id")
private String id;
@SerializedName("BaseName")
private String baseName;
@SerializedName("itemdesc")
private String itemdesc;
@SerializedName("subitems")
private ArrayList<SubItem> subItems;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBaseName() {
return baseName;
}
public void setBaseName(String baseName) {
this.baseName = baseName;
}
public String getItemdesc() {
return itemdesc;
}
public void setItemdesc(String itemdesc) {
this.itemdesc = itemdesc;
}
public ArrayList<SubItem> getSubItems() {
return subItems;
}
public void setSubItems(ArrayList<SubItem> subItems) {
this.subItems = subItems;
}
}
import com.google.gson.annotations.SerializedName;
public class SubItem {
@SerializedName("id")
private String id;
@SerializedName("SubItemdesc")
private String subItemdesc;
@SerializedName("SubItemprice")
private String subItemprice;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSubItemdesc() {
return subItemdesc;
}
public void setSubItemdesc(String subItemdesc) {
this.subItemdesc = subItemdesc;
}
public String getSubItemprice() {
return subItemprice;
}
public void setSubItemprice(String subItemprice) {
this.subItemprice = subItemprice;
}
}
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.apache.commons.io.IOUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class MyTest {
public static void main(String[] args) {
String stringurl = "http://www.yell4food.com/json/data_standard_item_new.php";
try {
URL url = new URL(stringurl);
URLConnection connection = url.openConnection();
connection.connect();
String result = IOUtils.toString(connection.getInputStream());
System.out.println("" + result);
ArrayList<Response> responses = (new Gson()).fromJson(result,
new TypeToken<ArrayList<Response>>() {
}.getType());
for (Response response : responses) {
System.out.println("" + response.getMenuName());
ArrayList<Items> items = response.getItems();
if (items != null) {
if (!items.isEmpty()) {
for (Items items2 : items) {
System.out.println("\t"+items2.getId());
System.out.println("\t"+items2.getBaseName());
System.out.println("\t"+items2.getItemdesc());
if (items3 != null) {
if (!items3.isEmpty()) {
for (SubItem subItem : items3) {
System.out.println("\t\t"+subItem.getId());
System.out.println("\t\t"+subItem.getSubItemdesc());
System.out.println("\t\t"+subItem.getSubItemprice());
}
}
}
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Beverages
1
Coca-Cola
1
0.33L
0.75
2
1.5L
1.85
2
Diet Coca-Cola
1
0.33L
0.75
2
1.5L
1.85