如何将已解析的JSON消息的内容存储到基于关键字的字符串变量中?

时间:2019-05-24 20:21:29

标签: java json jackson deserialization json-deserialization

我需要帮助,将经过解析的JSON消息中的文本存储到基于关键字的变量中。我的意思是,我有一次解析过的JSON消息,并且在该消息中,它返回如下内容:

Name: Kobe Bryant<br/>
Company: Lakers<br/>
Email: kobe@lakers.com<br/>
etc.

我希望能够看到这个测试块,看到它显示“ Name:Kobe Bryant”,并将Kobe Bryant存储到一个名为“ name”的字符串变量中,依此类推。我该如何处理?

public class ParseTest {
    public static void main(String[] args) throws Exception {

        String name;
        String company;
        String email;
        String phoneNumber;
        String projectType;
        String contactBy;
        String timeFrame;
        String message;

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

        MainParser mp = mapper.readValue(new File("/Users/kane/VersionControl/LambdaTest/src/InfoBox.txt"), MainParser.class);

        if ("blah".equals(mp.getTopicArn())) {
            //Send to proposal table
            System.out.println(mp.getSubject());
            System.out.println(mp.getMessage());
        } else if ("blah blah".equals(mp.getTopicArn())) {
            //Send to infoBox table
            System.out.println(mp.getMessage());
        }
    }
}

我正在解析的JSON是:

{
 "Subject" : "Proposal Request (sent Wed May 22 2019 14:47:49 GMT-0400 (Eastern Daylight Time))",
 "Message" : "Name: Kobe Bryant\nCompany: Lakers\nEmail: kobe@lakers.com"
}

这是POJO类:

private String Subject;
private String Message;

public String getSubject() {
    return Subject;
}

public void setSubject(String subject) {
    Subject = subject;
}

public String getMessage() {
    return Message;
}

public void setMessage(String message) {
    Message = message;
}

1 个答案:

答案 0 :(得分:0)

您有一个JSON有效负载,其中一个值包含成对的key:value对数据,并用新行\n分隔。我们需要创建两个POJO类:用于根Message的{​​{1}}和JSON-内部人员数据。这两个类可能看起来像这样:

Person

我们可以如下解析您的class Message { private String subject; private String message; // getters, setters, toString } class Person { private String name; private String company; private String email; // ... more properties // getters, setters, toString } 有效载荷:

JSON

上面的示例为您的import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import java.io.File; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; public class JsonApp { public static void main(String[] args) throws Exception { File jsonFile = new File("./resource/test.json").getAbsoluteFile(); ObjectMapper mapper = new ObjectMapper(); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE); Message message = mapper.readValue(jsonFile, Message.class); String[] lines = message.getMessage().split("\n"); Map<String, String> properties = Stream.of(lines) .map(i -> i.split(":")) .filter(i -> i.length == 2) .collect(Collectors.toMap(i -> i[0].trim(), i -> i[1].trim())); Person person = mapper.convertValue(properties, Person.class); System.out.println(person); } } 有效负载打印:

JSON

如您所见,我们根据内部数据创建了Person{name='Kobe Bryant', company='Lakers', email='kobe@lakers.com'} ,并使用convertValue方法将Map转换为Map类。

另请参阅:

  1. Java 8 – Convert List to Map
  2. How to deserialize a Map into POJO?
  3. How to serialize to java.util.List and java.util.Map with Jackson