【问题标题】:How to map toString value to the class?如何将 toString 值映射到类?
【发布时间】:2018-09-19 22:07:04
【问题描述】:

我有课。我已经覆盖了 toString 值。现在我想将 toString 输出的值映射到类。有什么捷径吗?

public class Cat {
    String name;
    int age;
    String color;

    public Cat(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Cat{" + "name=" + name + ", age="+age+",color="+color+'}';
    }
}`

简而言之,我想将以下值映射到 Cat 类

String value = "Cat{name=Kitty,age=1,color=Black}"
Cat cat = // create a 'Cat'from 'value'

提前谢谢你。

【问题讨论】:

  • 你的意思是创建Cat cat = new Cat("Kitty", 1, "Black"); 然后打印猫,你就得到了你想要的!
  • 你不需要做任何额外的事情,重写 toString() 就可以了。
  • “现在我想将 toString 输出的值映射到类” 这对我来说没有任何意义。请问能改一下吗?
  • 你说的映射是什么意思?
  • 您是否要创建特定的 Cat 实例?即一只一岁的黑猫,名叫凯蒂?或者您希望在调用 toString() 时始终得到相同的结果,而不管实际的属性值如何? (没有多大意义,但是嘿)

标签: java class mapping tostring


【解决方案1】:

您可以使用正则表达式来做到这一点:

Pattern catCapture = Pattern.compile(
        "Cat\\{name=(?<name>\\w*)\\s*,\\s*age=(?<age>\\d+)\\s*,\\s*color=(?<color>\\w*)\\}"
    );

String input = "Cat{name=Kitty,age=1,color=Black}";

Matcher matcher = catCapture.matcher(input);
Cat cat;
if(matcher.find()) {
    String name = matcher.group("name");
    int age = Integer.parseInt(matcher.group("age"));
    String color = matcher.group("color");
    cat = new Cat(name, age, color);
} else {
    throw new RuntimeException("Can not parse: " + input);
}

System.out.println(cat); // prints Cat{name=Kitty, age=1,color=Black}

正则表达式将名称、年龄和颜色这 3 个值捕获到 (?&lt;name&gt;\\w*)(?&lt;age&gt;\\d+)(?&lt;color&gt;\\w*) 组中,以便您随后获取它们。不过,您仍然必须将年龄解析为 int。

它还解释了逗号和值之间的不同数量的空白使用 \\s*(即 0 个或更多空白)。

另见这些:

【讨论】:

  • 非常感谢乔恩。它会为我工作。我正在寻找一些内置系统,如果有任何我可以更懒惰的地方:D。
【解决方案2】:

最后我想要的课程看起来像这个。特别感谢@Jorn vernee

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Cat {
    String name;
    int age;
    String color;

    public Cat(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public Cat(String strValue){

        Pattern catCapture = Pattern.compile(
            "Cat\\{name=(?<name>\\w*)\\s*,\\s*age=(?<age>\\d+)\\s*,\\s*color=(?<color>\\w*)\\}"
        );

        Matcher matcher = catCapture.matcher(strValue);

        if(matcher.find()) {
            this.name = matcher.group("name");
            this.age = Integer.parseInt(matcher.group("age"));
            this.color = matcher.group("color");
        } else {
            throw new RuntimeException("Can not parse: " + strValue);
        }
    }

    @Override
    public String toString() {
        return "Cat{" + "name=" + name + ", age=" + age + ", color=" + color + '}';
    }
}

【讨论】:

    【解决方案3】:

    根据您的要求考虑GSON

    来自user guide

    class BagOfPrimitives {
      private int value1 = 1;
      private String value2 = "abc";
      private transient int value3 = 3;
      BagOfPrimitives() {
        // no-args constructor
      }
    }
    
    // Serialization
    BagOfPrimitives obj = new BagOfPrimitives();
    Gson gson = new Gson();
    String json = gson.toJson(obj);  
    

    还有,反序列化,这是你的用例:

    // Deserialization
    BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
    

    【讨论】:

    • 嗯......它的工作原理。正是我想要的。谢谢@sujit。
    【解决方案4】:

    这就是我要找的……谢谢 sujit。

        Cat cat = new Cat("Kitty",1,"Black");
        Gson gson = new Gson();
        String json = gson.toJson(cat);  
        System.out.println(json);
    
        Cat cat2 = gson.fromJson(json, Cat.class);
        System.out.println(cat2.toString());
    

    【讨论】:

      猜你喜欢
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多