简体   繁体   English

将逗号分隔的键=值对转换为Java对象

[英]Convert comma separated key=value pairs to Java object

I'm trying to implement a HttpMessageConverter which would allow my program to talk over REST to an embedded smart controller. 我正在尝试实现HttpMessageConverter,它将允许我的程序通过REST与嵌入式智能控制器进行通信。

The controller responds with strings such as: 控制器用以下字符串响应:

ret=OK,htemp=27.0,hhum=-,otemp=27.0,err=0,cmpfreq=24

I have a Java object called SensorInfo. 我有一个名为SensorInfo的Java对象。

public class SensorInfo {

   String ret;
   Double htemp;
   String hhum;
   Double otemp;
   Integer err;
   Integer cmpfreq;

   // getters and setters

}

What's the best way of mapping the controller response to the above Java object? 将控制器响应映射到上述Java对象的最佳方法是什么?

You can simply split the string and assign each element as needed. 您可以简单地拆分字符串并根据需要分配每个元素。 You have: 你有:

ret=OK,htemp=27.0,hhum=-,otemp=27.0,err=0,cmpfreq=24

Lets assume you have that stored in a variable called myStr . 假设您已将其存储在名为myStr的变量中。 Then all you need to do is this: 然后,您需要做的就是:

String[] strSplit = myStr.split(" ");
SensorInfo info = new SensorInfo();
info.ret = afterEquals(strSplit[0]);
info.htemp = Double.parse(afterEquals(strsplit[1]));
info.hhum = afterEquals(strSplit[2]);
info.otemp= Double.parse(afterEquals(strSplit[3]));
info.err = Integer.parse(afterEquals(strSplit[4]));
info.cmpfreq = Integer.parse(afterEquals(strSplit[5]));

You will declare a method to extract the part of the response after the equals sign to make the above work: 您将声明一个方法,在等号后提取响应的一部分,以完成上述工作:

private String afterEquals(String input) {
  input.substring(input.indexOf('=') + 1);
}

Note that this assumes the order of your response is fixed. 请注意,这假设您的响应顺序是固定的。 If it isn't, you can easily modify this to look at each argument to see which variable to assign it to. 如果不是,则可以轻松地对其进行修改以查看每个参数,以查看将其分配给哪个变量。

You should add error handling, as the following is not really error prone, but might help you: 您应该添加错误处理,因为以下内容并不是很容易出错,但可能会帮助您:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ValueAssigner {
    // ret=OK,htemp=27.0,hhum=-,otemp=27.0,err=0,cmpfreq=24
    String ret;
    Double htemp;
    String hhum;
    Double otemp;
    Integer err;
    Integer cmpfreq;

    public static void main(String[] a) {
        System.out.println(new ValueAssigner("ret=OK,htemp=27.0,hhum=-,otemp=27.0,err=0,cmpfreq=24").getCmpfreq());
    }

    ValueAssigner(String in) {
        String[] split = in.split(",");
        for (String s : split) {
            Method method;
            String[] keyValue = s.split("=");
            try {
                method = this.getClass().getMethod("set" + ucFirst(keyValue[0]), String.class);
                method.invoke(this, keyValue[1]);
            } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException | SecurityException | NoSuchMethodException e) {
                // e.printStackTrace();
                // omitted here
            }

        }
    }

    private static String ucFirst(String in) {
        return in.substring(0, 1).toUpperCase() + in.substring(1);
    }


    public String getRet() {
        return ret;
    }

    public void setRet(String ret) {
        this.ret = ret;
    }

    public Double getHtemp() {
        return htemp;
    }

    public void setHtemp(String htemp) {
        this.htemp = Double.parse(htemp);
    }

    public String getHhum() {
        return hhum;
    }

    public void setHhum(String hhum) {
        this.hhum = hhum;
    }

    public Double getOtemp() {
        return otemp;
    }

    public void setOtemp(String otemp) {
        this.otemp = Double.parse(otemp);
    }

    public Integer getErr() {
        return err;
    }

    public void setErr(String err) {
        this.err = Integer.parse(err);
    }

    public Integer getCmpfreq() {
        return cmpfreq;
    }

    public void setCmpfreq(String cmpfreq) {
        this.cmpfreq = Integer.parse(cmpfreq);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM