String 转成List
//String的格式为
String b = "[{\"id\":\"1\",\"name\":\"test\",\"age\":\"11\"}]";
//转成实体list
List<Field> a = JSONArray.parseArray(b,Field.class);
实体类为
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class Field{
private String id;
private String name;
private String age;
}
实体list转成string
String jsonStr = JSON.toJSONString(a);
JSON JSONArray来源包为
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
另一个方法使用jackSon
List转成String
import com.fasterxml.jackson.databind.ObjectMapper;
String b = "[{\"id\":\"1\",\"name\":\"test\",\"age\":\"11\"}]";
ObjectMapper mapper = new ObjectMapper();
String str = mapper.writeValueAsString(b);
String转成List
List<Field> field = mapper.readValue(str, new TypeReference<List<Field>() {}));
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)