在下面的json中,我可以阅读" 类别"中的字段。使用以下android代码。我不知道如何阅读" effect_list" 元素" 4" 和" 1&#34 ; 。 " 4"和" 1"是增量和动态。我应该如何为此创建一个pojo类以及我的代码应该如何在 Main_get.java 中?
{
"categories":[
{
"mcategory_id":"4",
"mcategory_name":"Band"
},
{
"mcategory_id":"1",
"mcategory_name":"Basic Effects"
},
{
"mcategory_id":"3",
"mcategory_name":"Bg Image Card"
}
],
"effect_list":{
"4":[
{
"effects_id":"18",
"effects_name":"Band 1"
},
{
"effects_id":"19",
"effects_name":"Band 2"
}
],
"1":[
{
"effects_id":"1",
"effects_name":"Background Blur"
},
{
"effects_id":"4",
"effects_name":"Blemish Removal"
}
]
}
}
我使用以下代码来获取json数组"类别" 使用改造及其工作没有任何问题。现在我如何获得" effect_list" 字段?
Contact.java
public class Contact {
@SerializedName("mcategory_id")
@Expose
private String mcategory_id;
@SerializedName("mcategory_name")
@Expose
private String mcategory_name;
public String getmcategory_id() {
return name;
}
public void setmcategory_id(String name) {
this.name = name;
}
public String getmcategory_name() {
return email;
}
public void setmcategory_name(String email) {
this.email = email;
}
}
ApiService.java
public interface ApiService {
@GET("xyz.json")
Call<ContactList> getMyJSON();
}
ContactList.java
public class ContactList {
@SerializedName("contacts")
@Expose
public ArrayList<Contact> contacts = new ArrayList<>();
/**
* @return The contacts
*/
public ArrayList<Contact> getContacts() {
return contacts;
}
/**
* @param contacts The contacts
*/
public void setContacts(ArrayList<Contact> contacts) {
this.contacts = contacts;
}
}
RetroClient.java
public class RetroClient {
private static final String ROOT_URL = "http://abc.ab/";
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
/**
* Get API Service
*
* @return API Service
*/
public static ApiService getApiService() {
return getRetrofitInstance().create(ApiService.class);
}
}
MyContactAdapter.java
List<Contact> contactList;
Context context;
private LayoutInflater mInflater;
// Constructors
public MyContactAdapter(Context context, List<Contact> objects) {
super(context, 0, objects);
this.context = context;
this.mInflater = LayoutInflater.from(context);
contactList = objects;
}
@Override
public Contact getItem(int position) {
return contactList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
Contact item = getItem(position);
vh.textViewName.setText(item.getName());
vh.textViewEmail.setText(item.getEmail());
// Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);
Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);
// loading.dismiss();
return vh.rootView;
}
public static class ViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
public ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static ViewHolder create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new ViewHolder(rootView, imageView, textViewName, textViewEmail);
}
}
Main_get.java
public class Main_get extends AppCompatActivity {
/**
* Views
*/
private GridView listView;
private View parentView;
private ArrayList<Contact> contactList;
private MyContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Array List for Binding Data from JSON to this List
*/
contactList = new ArrayList<>();
parentView = findViewById(R.id.parentLayout);
/**
* Getting List and Setting List Adapter
*/
listView = (GridView) findViewById(R.id.listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Snackbar.make(parentView, contactList.get(position).getName() + " => " + contactList.get(position).getName().getBytes(), Snackbar.LENGTH_LONG).show();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(@NonNull final View view) {
//Creating an object of our api interface
ApiService api = RetroClient.getApiService();
/**
* Calling JSON
*/
Call<ContactList> call = api.getMyJSON();
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<ContactList>() {
@Override
public void onResponse(Call<ContactList> call, Response<ContactList> response) {
if(response.isSuccessful()) {
/**
* Got Successfully
*/
contactList = response.body().getContacts();
/**
* Binding that List to Adapter
*/
adapter = new MyContactAdapter( Main_get.this, contactList);
listView.setAdapter(adapter);
} else {
// Snackbar.make(parentView, R.string.string_some_thing_wrong, Snackbar.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<ContactList> call, Throwable t) {
dialog.dismiss();
}
});
}
});
}
}
我应该在这段代码中做些什么改变?
答案 0 :(得分:0)
您可以使用
Map<Integer, List<Effect>>
在整数中,您将获得索引(&#34; 1&#34;,&#34; 4&#34; ...),在列表中您将获得一系列效果。