-1

假设我有这个字符串:

For the AWSDataTransfer product, this is the public pricing plan.

Regarding data transfer across EC2 AZs:
In all AWS regions, inbound is $0.01/GB.
In all AWS regions, outbound is $0.01/GB.

我想将其更改为 JSON,如下所示:

{
    "Product" : "AWSDataTransfer",
    "PlanName" : "public",

    "EC2Regional" : [
        {"region" : "all", "type" : "in", "rate" : "0.01/GB"},
        {"region" : "all", "type" : "out", "rate" : "0.01/GB"}
    ],
}

我如何使用正则表达式来做到这一点。任何人都可以编写代码并帮助我。

4

1 回答 1

2

您可以创建一个或两个包含您描述的字段的简单类,例如:

public class Plan {
    private String product;
    private String planName;
    private List<EC2Regional> ec2Regional;
}

public class EC2Regional {
    private String region;
    private String type;
    private String rate;
}

(为了便于阅读,我省略了 getter 和 setter。)然后您可以使用像 Jackson 这样的 JSON 库将其序列化为 JSON。

您可能想用枚举或其他自定义类型替换一些 String 类型。

祝你好运。

于 2013-06-10T21:02:33.050 回答