fastjson将json字符串转化成bean对象解析出错的检查方法

我的情况是:解析第一层数据成功,解析第二层嵌套的数据失败。如:

{
  "response": {
    "resultcode": "0",
    "errormsg": "查询成功",
    "poifrid": "3535353",
    "cardno": "545353535",
    "name": "gesrresge",
    "sex": "1",
    "birthday": "refwrefwr",
    "cardstatus": "0",
    "identityid": "6464646",
    "phone": "4353422"
  }
}

 

TestPerson person2 = JSON.parseObject(jsonObj.toString().toLowerCase(), TestPerson.class);

  

我的解决方法是:

1、检查内部类是不是static的。

2、检查有没有写构造方法。

3、先将bean对象转化成json字符串输出,将json字符串和自己的字符串做对比,看看哪里不一样。

        TestPerson.Response response2 = new TestPerson.Response();
                response2.setSex("56");
                response2.setName("rg4g");
                response2.setSex("565");
                response2.setBirthday("190231313");
                response2.setCardno("t4t43t");
                response2.setCardstatus("3ffg3");
                response2.setErrormsg("uj67j764");
                response2.setIdentityid("54gg4");
                response2.setPatientid("99707");
                response2.setPhone("5t4t45");
                response2.setResultcode("0");
                TestPerson person = new TestPerson();
                person.setResponse(response2);
                Log.e("bean2json-string:",JSON.toJSONString(person));

 TestPerson.java如下:

package com.kevinchan.fangding.Fragment;

import android.util.Log;

import java.io.Serializable;

/**
 * Created by Jackie on 2016/12/13.
 */

public class TestPerson implements Serializable {

    public TestPerson() {
//        setResponse2(this.Response2);
    }

    private Response response;

    public void setResponse(Response response){
        this.response = response;
    }
    public Response getResponse(){
        return this.response;
    }


    public static class Response {
        public Response(){

        }

        private String resultcode;

        private String errormsg;

        private String patientid;

        private String cardno;

        private String name;

        private String sex;

        private String birthday;

        private String cardstatus;

        private String identityid;

        private String phone;

        public void setResultcode(String resultcode){
            this.resultcode = resultcode;
        }
        public String getResultcode(){
            return this.resultcode;
        }
        public void setErrormsg(String errormsg){
            this.errormsg = errormsg;
        }
        public String getErrormsg(){
            return this.errormsg;
        }
        public void setPatientid(String patientid){
            this.patientid = patientid;
        }
        public String getPatientid(){
            return this.patientid;
        }
        public void setCardno(String cardno){
            this.cardno = cardno;
        }
        public String getCardno(){
            return this.cardno;
        }
        public void setName(String name){
            this.name = name;
        }
        public String getName(){
            return this.name;
        }
        public void setSex(String sex){
            this.sex = sex;
        }
        public String getSex(){
            return this.sex;
        }
        public void setBirthday(String birthday){
            this.birthday = birthday;
        }
        public String getBirthday(){
            return this.birthday;
        }
        public void setCardstatus(String cardstatus){
            this.cardstatus = cardstatus;
        }
        public String getCardstatus(){
            return this.cardstatus;
        }
        public void setIdentityid(String identityid){
            this.identityid = identityid;
        }
        public String getIdentityid(){
            return this.identityid;
        }
        public void setPhone(String phone){
            this.phone = phone;
        }
        public String getPhone(){
            return this.phone;
        }

    }
}

 注意:bean对象传递时类要序列化,实现Serializable类。implements Serializable。同时内部类也一定要序列化。

 

转载于:https://www.cnblogs.com/Jackie-zhang/p/6198288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 应用程序中使用 fastjson解析本地 JSON 文件为 Java Bean,可以按照以下步骤进行: 1. 引入 fastjson 库 在你的项目中添加 fastjson 库的依赖。可以通过 Gradle、Maven 或手动导入 JAR 包的方式进行添加。 2. 创建 Java Bean 创建一个 Java Bean,用于存储 JSON 文件中的数据。 ```java public class ExampleBean { private String name; private int age; private List<String> hobbies; // getter 和 setter 方法 } ``` 3. 解析 JSON 文件 使用 fastjson 库将本地 JSON 文件解析Java Bean。以下是一个简单的示例代码,假设你的 JSON 文件名为 "example.json": ```java try { InputStream is = getAssets().open("example.json"); // 打开文件输入流 int size = is.available(); // 获取文件大小 byte[] buffer = new byte[size]; // 创建一个缓冲区 is.read(buffer); // 将文件内容读入缓冲区 is.close(); // 关闭文件输入流 String json = new String(buffer, "UTF-8"); // 将缓冲区内的字节换为字符串 ExampleBean bean = JSON.parseObject(json, ExampleBean.class); // 解析 JSON 字符串Java Bean // 使用解析出来的 Java Bean 进行相关操作 String name = bean.getName(); int age = bean.getAge(); List<String> hobbies = bean.getHobbies(); // ... } catch (IOException e) { e.printStackTrace(); } ``` 在上述代码中,我们首先打开文件输入流,然后将文件内容读入缓冲区,接着将缓冲区内的字节换为字符串,最后使用 fastjson 库将 JSON 字符串解析Java Bean。需要注意的是,此处我们将文件内容读入缓冲区时使用了 UTF-8 编码,因此在创建 String 对象时也需要指定相应的编码方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值