mysql中的批量更新

使用Dubbo注解@Reference 注入service,service为null

  返回  

各种Json的基本转换(json和对象的互转)

2021/8/20 11:24:09 浏览:

各种JSON的maven引用

实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private String password;
}

一、jackjson maven

 <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.8</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.8.8</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.8</version>
    </dependency>

对象转json

 User user=new User("lish","ashdj");
        ObjectMapper objectMapper=new ObjectMapper();
        String str=objectMapper.writeValueAsString(user);
        System.out.println(str);

json转对象

//将json格式的字符串转化为对象
        //字符串中含有双引号,需要使用反斜杠转义
        String jsonstr="{\"password\":\"66666\",\"name\":\"dme\"}";
         ObjectMapper objectMapper=new ObjectMapper();
        User user=objectMapper.readValue(jsonstr,User.class);
        System.out.println(user.toString());

二、fastjson maven

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.4</version>
</dependency>

对象转json

User user1=new User("liul","pass");
        String str1= JSON.toJSONString(user1);
        System.out.println("简单java类转json字符串:"+str1);

json字符串转对象

String jsonstr="{'password':'123456','name':'dmego'}";
        User user2=JSON.parseObject(jsonstr,User.class);
        System.out.println(user2.toString());

三、Gson maven

 <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.2</version>
    </dependency>

对象转json

  //Gson
        User user3=new User("dijia","aishh");
        Gson gson=new Gson();
        String string=gson.toJson(user3);
        System.out.println(user3);

json转对象

String gsonstr="{'password':'456','name':'dddd'}";
Gson gson=new Gson();
        User user4=gson.fromJson(gsonstr,User.class);
        System.out.println(user4.toString());

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号