android下如何将jsonapi中的“item\u name”提取到java中

x4shl7ld  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(430)

我正在尝试将json数据解析到我的android java代码中,我能够从json中获取数据,但是我无法从中获取特定字段。
json api的结构如下:

[
  {
    "S/L NO": "1",
    "Item Name": "Cheese Corn",
    "Category": "Appetizers"
  },
  {
    "S/L NO": "2",
    "Item Name": "Pasta",
    "Category": "Full Meal"
  }
]

我想得到“项目名称”并加载到我的滚动微调器。以下是我的代码:

public class Provide_food extends AppCompatActivity {

    Spinner spinner;
    String url = "https://sheet.best/api/sheets/00f2c956-1c25-44ab-85f5-2e3bcfc580a0";
    List<Item_Model> list = new ArrayList<>();
    List<String> list1 = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provide_food);

        spinner = (Spinner) findViewById(R.id.select_food);

        List itemlist = new ArrayList<>();
        RequestQueue queue = Volley.newRequestQueue(this);

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.e("Response", response.toString());
                        CustomAdapter adapter = new CustomAdapter(Provide_food.this, R.layout.item, itemlist);
                        spinner.setAdapter(adapter);
                        // do something with the response
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO: Handle error
                    }
                });

        // Add request to queue
        queue.add(jsonArrayRequest);
    }
}

我还开发了一个customadapter,用于将项目名称加载到可滚动微调器中。代码如下:

package com.example.helping_hands_individual;

import android.content.Context;
import android.transition.Slide;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class CustomAdapter extends ArrayAdapter {

    LayoutInflater inflater;
    Context context;
    List<Item_Model> list;

    public CustomAdapter(Context applicationContext,int resource, List<Item_Model> list){
        super(applicationContext,resource);
        this.context = applicationContext;
        this.list = list;
        inflater = (LayoutInflater.from(applicationContext));
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflater.inflate(R.layout.item,null);
        TextView itemname = (TextView)view.findViewById(R.id.item_names);

        itemname.setText(list.get(i).itemname);

        return view;
    }
}

下面是item.xml的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/item_names"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:textSize="18dp"
        android:text="Demo"/>

</LinearLayout>

有人能帮我吗?

j2qf4p5b

j2qf4p5b1#

遍历jsonarray并使用 getString() 方法从对象获取字段值。

jsonObject.getString('Item Name');
dfty9e19

dfty9e192#

尝试此解决方案:
第一步。为你的React做一个模型。

public class Example {

    @SerializedName("S/L NO")
    @Expose
    private String sLNO;
    @SerializedName("Item Name")
    @Expose
    private String itemName;
    @SerializedName("Category")
    @Expose
    private String category;

    public String getSLNO() {
        return sLNO;
    }

    public void setSLNO(String sLNO) {
        this.sLNO = sLNO;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
}

第二步。把你的答案放到模型课上

private void parseJSON() {
    Gson gson = new Gson();
    Type type = new TypeToken<List<Example>>() {
    }.getType();
    List<Example> contactList = gson.fromJson(jsonString, type);
    for (Example example : contactList) {
        // get your data here like example.itemName
    }
}
m1m5dgzv

m1m5dgzv3#

public void onResponse(JSONArray response) {
Log.e("Response", response.toString());

      JSONObject object = new JSONObject(response);
      JSONArray array = object.getJSONArray("suppliers");
      for (int i = 0; i < array.length(); i++) {
           JSONObject object1 = array.getJSONObject(i);
           Log.e("Your Array", String.valueOf(object1));

           //get your itas value here by
           jsonObject.getString('Item Name');

      }

}

相关问题