关于JsonArray转换为List的问题

怀谨 2016-10-19 03:12:11
各位前辈好,请教一个问题。

String str = result.get();
JSONArray json = JSONArray.fromObject(str);
List<WorkOrder> list = new ArrayList<>();
list = (List)JSONArray.toCollection(json,WorkOrder.class);

每次程序运行到list = (List)JSONArray.toCollection(json,WorkOrder.class);这里的时候就会结束运行。没有任何说明以及错误提示。

...全文
2945 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
挖一下坟贴,我今天也碰到了这个问题,后来我看到了一个这样的解释:多表中含有相同字段(你的两个表都有id这个字段),转换就会出错。
hersing 2016-10-20
  • 打赏
  • 举报
回复
引用 6 楼 u012570392 的回复:
由于时间原因我就不纠结了,我使用这种方式。

try{
            String str = result.get();
            JSONArray jsonArray = new JSONArray(str);
            for(int i = 0; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                WorkOrder wo = new WorkOrder();
                wo.Id = jsonObject.getInt("Id");
                wo.CustomerComplaint = jsonObject.getString("CustomerComplaint");
                wo.Type = jsonObject.getInt("Type");
                wo.Status = jsonObject.getInt("Status");
                wo.CreationTime = jsonObject.getString("CreationTime");
                wo.CreatedBy = jsonObject.getString("CreatedBy");
                wo.ProcessOn = jsonObject.getString("ProcessOn");
                wo.ProcessBy = jsonObject.getString("ProcessBy");
                mData.add(wo);
            }
        }catch (ExecutionException e){
        }catch (InterruptedException e){
昨天有事在忙,没及时给你回复,打算重新模拟下你的代码然后敲一下的,抱歉!
怀谨 2016-10-20
  • 打赏
  • 举报
回复
由于时间原因我就不纠结了,我使用这种方式。

try{
            String str = result.get();
            JSONArray jsonArray = new JSONArray(str);
            for(int i = 0; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                WorkOrder wo = new WorkOrder();
                wo.Id = jsonObject.getInt("Id");
                wo.CustomerComplaint = jsonObject.getString("CustomerComplaint");
                wo.Type = jsonObject.getInt("Type");
                wo.Status = jsonObject.getInt("Status");
                wo.CreationTime = jsonObject.getString("CreationTime");
                wo.CreatedBy = jsonObject.getString("CreatedBy");
                wo.ProcessOn = jsonObject.getString("ProcessOn");
                wo.ProcessBy = jsonObject.getString("ProcessBy");
                mData.add(wo);
            }
        }catch (ExecutionException e){
        }catch (InterruptedException e){
怀谨 2016-10-19
  • 打赏
  • 举报
回复
引用 4 楼 hersing 的回复:
WorkOrder贴出来
这是我webapi 的

public class WorkOrder
    {
        public int Id { get; set; }

        public string CaseNumber { get; set; }

        public string Name { get; set; }

        public string Phone { get; set; }

        public string Email { get; set; }

        public string CustomerComplaint { get; set; }

        public int Type { get; set; }

        public int Status { get; set; }

        public decimal Price { get; set; }

        public string Vip { get; set; }

        public string CsSummary { get; set; }

        public string TreatmentResult { get; set; }

        public DateTime CreationTime { get; set; }

        public string CreatedBy { get; set; }

        public string ProcessOn { get; set; }

        public string ProcessBy { get; set; }
    }
这是我 android 的

public class WorkOrder {

    public int Id;

    public String CaseNumber;

    public String Name;

    public String Phone;

    public String Email;

    public String CustomerComplaint;

    public int Type;

    public int Status;

    public Double Price;

    public String Vip;

    public String CsSummary;

    public String TreatmentResult;

    public String CreationTime;

    public String CreatedBy;

    public String ProcessOn;

    public String ProcessBy;

}

hersing 2016-10-19
  • 打赏
  • 举报
回复
WorkOrder贴出来
怀谨 2016-10-19
  • 打赏
  • 举报
回复
引用 2 楼 hersing 的回复:
原因:返回的json数据字段名称跟你需转换的Model,也就是WorkOrder字段不统一
字段一样的呀,我是复制过来的!
hersing 2016-10-19
  • 打赏
  • 举报
回复
原因:返回的json数据字段名称跟你需转换的Model,也就是WorkOrder字段不统一
怀谨 2016-10-19
  • 打赏
  • 举报
回复
大召唤术:召唤大神

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧