4
回答
如何将字符串转换成javaBean
百度AI开发者大赛带你边学边开发,赢100万奖金,加群:418589053   

{
    "data": {
        "TaskManager": {
            "associatesId": 10019, 
            "associatesName": "10019", 
            "avatar": "http://timg.xmggg.com/numberPic/201511/32641447667858212.png", 
            "complaintsId": 0, 
            "complaintsName": "", 
            "copeMoney": 400, 
            "createDate": 1452064186000, 
            "endDate": 1452064186000, 
            "lastUpdateDate": 1454248276000, 
            "money": 380, 
            "name": "小师", 
            "startDate": 1452064186000, 
            "tArea": "宁波市 鄞州区 南部商务区", 
            "tDiscription": "收一块二手的 NVIDIA GTX750TI显卡,要显存有2G的,没坏过,没修过的,最多用半年以内,要有发票", 
            "tState": 2, 
            "tStateStr": "发布中", 
            "tid": 27, 
            "timg1": "", 
            "timg2": "", 
            "timg3": "", 
            "timg4": "", 
            "tmid": "2016010615094675716", 
            "toAccountMoneyDate": 1452064186000, 
            "types": 2, 
            "typesStr": "追加任务", 
            "uid": 10011
        }, 
        "bidderAvatar": [
            {
                "avatar": "http://timg.xmggg.com/numberPic/201511/32641447667858212.png", 
                "uid": 10020
            }
        ], 
        "bidderList": [
            {
                "avatar": "http://timg.xmggg.com/numberPic/201511/32641447667858212.png", 
                "cDate": 1453430422000, 
                "cName": "小师", 
                "content": "啦啦啦 德玛西亚", 
                "tcid": 13, 
                "tmid": "2016010615094675716", 
                "uid": 10011
            }, 
            {
                "cDate": 1452844235000, 
                "content": "啦啦啦 德玛西亚", 
                "tcid": 7, 
                "tmid": "2016010615094675716", 
                "uid": 10001
            },  
            {
                "avatar": "http://timg.xmggg.com/numberPic/201511/32641447667858212.png", 
                "cDate": 1452133637000, 
                "cName": "껦袑", 
                "content": "750ti不好,没有980能玩游戏?", 
                "tcid": 2, 
                "tmid": "2016010615094675716", 
                "uid": 10013
            }, 
            {
                "avatar": "http://timg.xmggg.com/numberPic/201511/32641447667858212.png", 
                "cDate": 1452133581000, 
                "cName": "小师", 
                "content": "这么点钱就想收750ti,你肯定想多了", 
                "tcid": 1, 
                "tmid": "2016010615094675716", 
                "uid": 10011
            }
        ], 
        "bidderNumber": 2, 
        "publisherName": "小师"
    }, 
    "msg": "查询成功", 
    "status": 200
}

请高手指教

<无标签>
举报
樱花泪
发帖于2年前 4回/980阅
共有4个答案 最后回答: 2年前
1,先写好你的bean,{}的是对象,你需要写个对应的bean,[]的测试集合,List就行

2,导入fastjson包,

3,写个类测试下,JSON.pare(String str)好像是这个方法,自己查下,也就那几个api

--- 共有 2 条评论 ---
北风刮的不认真了回复 @樱花泪 : 楼下好有时间,居然直接给你上代码了 2年前 回复
樱花泪bidderList、bidderAvatar 两个无法转换 有什么办法吗 2年前 回复
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.3</version>
</dependency>

-------------------------------------------------------------------------------
package test;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import domain.Address;
import domain.User;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Jackson {
    public static void main(String[] args) throws IOException {
        Address address = new Address();
        address.setId(1010);
        address.setStreet("xyz");

        User user = new User();
        user.setUsername("Biao");
        user.setGender(User.Gender.FEMALE);
        user.setAddress(address);

        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 不序列化 null
        
        // Write object as string
        String json = mapper.writeValueAsString(user);
        System.out.println("1. " + json);
        // System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user)); // Pretty output

        // Read object from Json string
        user = mapper.readValue(json, User.class);
        System.out.println("2. " + user);

        // Write map as string
        Map<String, Object> map = new ConcurrentHashMap<String, Object>();
        map.put("user1", user);
        System.out.println("3. " + mapper.writeValueAsString(map));
        
        // Read json to Map
        // mapper.readValue(json, new TypeReference<Map<String, Object>>(){});

        // Read from tree(map)
        JsonNode tree = mapper.readTree(json);
        System.out.println("4. " + tree.get("address").get("id"));
    }
}

-------------------------------------------------------------------------------

package domain;


public class User {
    public enum Gender {
        MALE, FEMALE
    }
    
    private int id;
    private String username;
    private String password;
    private Gender gender;
    private Address address;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender=" + gender +
                ", address=" + address +
                '}';
    }
}

-------------------------------------------------------------------------------

package domain;

public class Address {
    private int id;
    private String street;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", street='" + street + '\'' +
                '}';
    }
}



顶部