如何将String值转换为json?

问题描述 投票:0回答:1

我有一串用逗号和“=”分隔的值。我需要将其转换为适当的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"}}]
java json
1个回答
0
投票
String value = arrayObject.get(VALUE).toString(); 
JSONArray array = new JSONArray(value); 
© www.soinside.com 2019 - 2024. All rights reserved.