【问题标题】:Parse Dynamic json object in Android在 Android 中解析动态 json 对象
【发布时间】:2016-12-29 09:51:08
【问题描述】:

我正在尝试解析以下json 并将其显示在ListView 中使用BaseAdapter,Android。这里的对象 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;
}
}

EffectList.java

public class EffectList {



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

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



 //GETTERS AND SETTERS


} 

MyContactAdapter2.java

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 android json getjson baseadapter


【解决方案1】:

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

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

希望有帮助

【讨论】:

    【解决方案2】:

    Have a look at this

    我在使用 GSON 解析时也遇到了同样的问题,但是通过使用 StringRequest 而不是 GSONRequest 来实现 请检查我的答案。

    【讨论】:

      【解决方案3】:

      您可以从本网站的JSON 创建POJO Model class

      http://www.jsonschema2pojo.org/

      这是我从您的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;
      }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-26
        • 1970-01-01
        • 1970-01-01
        • 2021-04-08
        • 2013-12-20
        • 1970-01-01
        相关资源
        最近更新 更多