Java 在Android中解析动态json对象

Java 在Android中解析动态json对象,java,android,json,getjson,baseadapter,Java,Android,Json,Getjson,Baseadapter,我正在尝试解析以下json,并使用Android的BaseAdapter在ListView中显示它。这里的对象4和1是动态的。如果这些值不是动态的,我知道如何创建模型类。我尝试使用以下方法获取json,但它不返回任何值Model.java中是否有任何错误?还是应该更改json的格式? { "effect_list":[ { "4":[ { "effects_id":"1

我正在尝试解析以下
json
,并使用Android的
BaseAdapter
ListView
中显示它。这里的对象41动态的。如果这些值不是动态的,我知道如何创建模型类。我尝试使用以下方法获取
json
,但它不返回任何值Model.java中是否有任何错误?还是应该更改json的格式?

         {  
   "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"
            }
         ]
      }
   ]
}
Model.java

public class Model{


@SerializedName("effect_list")
@Expose
List<Map<String,List<EffectList>>>  effectlist;

public List<Map<String, List<EffectList>>> getEffectlist() {
    return effectlist;
}

public void setEffectlist(List<Map<String, List<EffectList>>> effectlist) {
    this.effectlist = effectlist;
}
}
public class EffectList {



@SerializedName("effects_id")
@Expose
private String effectsId;

@SerializedName("effects_name")
@Expose
private String effectsName;



 //GETTERS AND SETTERS


} 
public class MyContactAdapter2 extends BaseAdapter {


    ArrayList<Map<String, List<EffectList>>> contactList;
    Context context;
    private LayoutInflater mInflater;

public MyContactAdapter2(Context context, ArrayList<Map<String, List<EffectList>>> data) {      
    this.context = context;
    this.mInflater = LayoutInflater.from(context);
    contactList = data;


}


@Override
public int getCount() {
    return 0;
}

@Override
public List<HashMap<String, List<EffectList>>> getItem(int position) {
    return (List<HashMap<String, List<EffectList>>>) contactList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder1 vh1;
    if (convertView == null) {
        View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
        vh1 = ViewHolder1.create((RelativeLayout) view);
        view.setTag(vh1);
    } else {
        vh1 = (ViewHolder1) convertView.getTag();
    }


    EffectList item = (EffectList) getItem(position);


    //   vh.textViewName.setText(item.getEffectsId());


    vh1.textViewName.setText(item.getEffectsName());
    vh1.textViewEmail.setText(item.getEffectsId());

    // Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);

    return vh1.rootView;
}


private static class ViewHolder1 {
    public final RelativeLayout rootView;
    public final ImageView imageView;
    public final TextView textViewName;
    public final TextView textViewEmail;

    private ViewHolder1(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
        this.rootView = rootView;
        this.imageView = imageView;
        this.textViewName = textViewName;
        this.textViewEmail = textViewEmail;
    }

    public static MyContactAdapter2.ViewHolder1 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 MyContactAdapter2.ViewHolder1(rootView, imageView, textViewName, textViewEmail);
    }
}


}
MyContactAdapter2.java

public class Model{


@SerializedName("effect_list")
@Expose
List<Map<String,List<EffectList>>>  effectlist;

public List<Map<String, List<EffectList>>> getEffectlist() {
    return effectlist;
}

public void setEffectlist(List<Map<String, List<EffectList>>> effectlist) {
    this.effectlist = effectlist;
}
}
public class EffectList {



@SerializedName("effects_id")
@Expose
private String effectsId;

@SerializedName("effects_name")
@Expose
private String effectsName;



 //GETTERS AND SETTERS


} 
public class MyContactAdapter2 extends BaseAdapter {


    ArrayList<Map<String, List<EffectList>>> contactList;
    Context context;
    private LayoutInflater mInflater;

public MyContactAdapter2(Context context, ArrayList<Map<String, List<EffectList>>> data) {      
    this.context = context;
    this.mInflater = LayoutInflater.from(context);
    contactList = data;


}


@Override
public int getCount() {
    return 0;
}

@Override
public List<HashMap<String, List<EffectList>>> getItem(int position) {
    return (List<HashMap<String, List<EffectList>>>) contactList.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder1 vh1;
    if (convertView == null) {
        View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
        vh1 = ViewHolder1.create((RelativeLayout) view);
        view.setTag(vh1);
    } else {
        vh1 = (ViewHolder1) convertView.getTag();
    }


    EffectList item = (EffectList) getItem(position);


    //   vh.textViewName.setText(item.getEffectsId());


    vh1.textViewName.setText(item.getEffectsName());
    vh1.textViewEmail.setText(item.getEffectsId());

    // Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);

    return vh1.rootView;
}


private static class ViewHolder1 {
    public final RelativeLayout rootView;
    public final ImageView imageView;
    public final TextView textViewName;
    public final TextView textViewEmail;

    private ViewHolder1(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
        this.rootView = rootView;
        this.imageView = imageView;
        this.textViewName = textViewName;
        this.textViewEmail = textViewEmail;
    }

    public static MyContactAdapter2.ViewHolder1 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 MyContactAdapter2.ViewHolder1(rootView, imageView, textViewName, textViewEmail);
    }
}


}
公共类MyContactAdapter2扩展BaseAdapter{
ArrayList联系人列表;
语境;
私人停车场;
公共MyContactAdapter2(上下文,数组列表数据){
this.context=上下文;
this.mInflater=LayoutInflater.from(上下文);
联系人列表=数据;
}
@凌驾
public int getCount(){
返回0;
}
@凌驾
公共列表getItem(int位置){
返回(列表)联系人列表。获取(位置);
}
@凌驾
公共长getItemId(int i){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
最终视图持有者1 vh1;
if(convertView==null){
视图视图=mInflater.flate(R.layout.get\u layout\u row\u视图,父视图,false);
vh1=ViewHolder1.create((RelativeLayout)视图);
视图.设置标签(vh1);
}否则{
vh1=(ViewHolder1)convertView.getTag();
}
EffectList项=(EffectList)getItem(位置);
//vh.textViewName.setText(item.getEffectsId());
vh1.textViewName.setText(item.getEffectsName());
vh1.textViewEmail.setText(item.getEffectsId());
//Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);
返回vh1.rootView;
}
私有静态类ViewHolder1{
公共最终相对论Yout rootView;
公共最终图像视图;
公共最终文本视图文本视图名称;
公开最终文本查看文本查看电子邮件;
private ViewHolder1(RelativeLayout rootView、ImageView ImageView、TextView textViewName、TextView textViewEmail){
this.rootView=rootView;
this.imageView=imageView;
this.textViewName=textViewName;
this.textViewEmail=textViewEmail;
}
公共静态MyContactAdapter2.ViewHolder1创建(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);
返回新的MyContactAdapter2.ViewHolder1(rootView、imageView、textViewName、textViewEmail);
}
}
}

MyContactAdapter2有什么问题吗?

我认为你应该改变这种方法

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

希望这有帮助

我认为你应该改变这种方法

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

希望此帮助

您可以从此网站从
JSON
创建
POJO模型类

我是根据你的
json
制作的!尽管你的
json
应该更简单

-----------------------------------com.example.EffectList.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class EffectList {

@SerializedName("4")
@Expose
private List<com.example._4> _4 = null;
@SerializedName("1")
@Expose
private List<com.example._1> _1 = null;

public List<com.example._4> get4() {
return _4;
}

public void set4(List<com.example._4> _4) {
this._4 = _4;
}

public List<com.example._1> get1() {
return _1;
}

public void set1(List<com.example._1> _1) {
this._1 = _1;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("effect_list")
@Expose
private List<EffectList> effectList = null;

public List<EffectList> getEffectList() {
return effectList;
}

public void setEffectList(List<EffectList> effectList) {
this.effectList = effectList;
}

}
-----------------------------------com.example._1.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class _1 {

@SerializedName("effects_id")
@Expose
private String effectsId;
@SerializedName("effects_name")
@Expose
private String effectsName;

public String getEffectsId() {
return effectsId;
}

public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}

public String getEffectsName() {
return effectsName;
}

public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}

}
-----------------------------------com.example._4.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class _4 {

@SerializedName("effects_id")
@Expose
private String effectsId;
@SerializedName("effects_name")
@Expose
private String effectsName;

public String getEffectsId() {
return effectsId;
}

public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}

public String getEffectsName() {
return effectsName;
}

public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}

}
package.com.example;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类效应列表{
@序列化名称(“4”)
@暴露
私有列表_4=null;
@序列化名称(“1”)
@暴露
私有列表_1=null;
公共列表get4(){
返回4;
}
公共无效集合4(列表4){
这个._4=_4;
}
公共列表get1(){
返回1;
}
公共无效集合1(列表1){
这个._1=_1;
}
}
-----------------------------------com.example.example.java-----------------------------------
包com.example;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公开课范例{
@SerializedName(“效果列表”)
@暴露
私有列表effectList=null;
公共列表getEffectList(){
返回效应列表;
}
public void setEffectList(List effectList){
this.effectList=effectList;
}
}
-----------------------------------com.example._1.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类1{
@SerializedName(“效果id”)
@暴露
私有字符串效应SID;
@SerializedName(“效果名称”)
@暴露
私有字符串效应器名称;
公共字符串getEffectsId(){
返回效应SID;
}
public void setEffectsId(字符串effectsId){
this.effectsId=effectsId;
}
公共字符串getEffectsName(){
返回有效值sname;
}
public void setEffectsName(字符串effectsName){
this.effectsName=effectsName;
}
}
-----------------------------------com.example._4.java-----------------------------------
包com.example;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类4{
@SerializedName(“效果id”)
@暴露
私有字符串效应SID;
@SerializedName(“效果名称”)
@暴露
私有字符串效应器名称;
公共字符串getEffectsId(){
返回效应SID;
}
public void setEffectsId(字符串effectsId){
this.effectsId=effectsId;
}
公共字符串getEffectsName(){
返回有效值sname;
}
public void setEffectsName(字符串effectsName){
this.effectsName=effectsName;
}
}

您可以从此网站从
JSON
创建
POJO模型类

我是根据你的
json
制作的!尽管你的
json
应该更简单

-----------------------------------com.example.EffectList.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class EffectList {

@SerializedName("4")
@Expose
private List<com.example._4> _4 = null;
@SerializedName("1")
@Expose
private List<com.example._1> _1 = null;

public List<com.example._4> get4() {
return _4;
}

public void set4(List<com.example._4> _4) {
this._4 = _4;
}

public List<com.example._1> get1() {
return _1;
}

public void set1(List<com.example._1> _1) {
this._1 = _1;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("effect_list")
@Expose
private List<EffectList> effectList = null;

public List<EffectList> getEffectList() {
return effectList;
}

public void setEffectList(List<EffectList> effectList) {
this.effectList = effectList;
}

}
-----------------------------------com.example._1.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class _1 {

@SerializedName("effects_id")
@Expose
private String effectsId;
@SerializedName("effects_name")
@Expose
private String effectsName;

public String getEffectsId() {
return effectsId;
}

public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}

public String getEffectsName() {
return effectsName;
}

public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}

}
-----------------------------------com.example._4.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class _4 {

@SerializedName("effects_id")
@Expose
private String effectsId;
@SerializedName("effects_name")
@Expose
private String effectsName;

public String getEffectsId() {
return effectsId;
}

public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}

public String getEffectsName() {
return effectsName;
}

public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}

}
package.com.example;
导入java.util.List;
导入com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类效应列表{
@序列化名称(“4”)
@暴露
私有列表_4=null;
@序列化DN