如何将String值转换为json?

时间:2019-03-28 08:38:10

标签: java json

我有一串用逗号和“ =”隔开的值。我需要将其转换为正确的json格式:

value = value.replace("=", ":");
String [] arrayStr=value.split(",");

JsonObjectBuilder builder = Json.createObjectBuilder();
String key = null;
for (String s: arrayStr){
    if(key == null) {
        key = s;
    } else {
        builder.add(key, s);
        key = null;
    }
}
JsonObject val = builder.build();

以下是我所拥有的内容以及我想如何对其进行修改:

[{Locale=en, Address={House=138, Street=Whitefield Road, CityDistrict=Bangalore, City=Bangalore, District=Bangalore, RegionCode=KA, Region=Whitefield, CountryCode=IND, Country=India, PostalCode=560066}}, {Locale=de, Address={House=138, Street=Whitefield Road, CityDistrict=Bangalore, City=Bangalore, District=Bangalore, RegionCode=KA, Region=Whitefield, CountryCode=IND, Country=India, PostalCode=560066}}]

[{"Locale":"en","Address":{"House":"138","Street":"Whitefield Road","CityDistrict":"Bangalore","City":"Bangalore","District":"Bangalore","RegionCode":"KA","Region":"Whitefield","CountryCode":"IND","Country":"India","PostalCode":"560066"}},{"Locale":"de","Address":{"House":"138","Street":"Whitefield Road","CityDistrict":"Bangalore","City":"Bangalore","District":"Bangalore","RegionCode":"KA","Region":"Whitefield","CountryCode":"IND","Country":"India","PostalCode":"560066"}}]

1 个答案:

答案 0 :(得分:0)

ticks = '[{"Id": 1}, {"Id": 2}, {"Id": 3}]';