简体   繁体   中英

How to convert the String value into json?

I have a string of values separated by comma and "=". I need to convert this into proper json format:

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();

Here is the content that I have and how I would like to conert it:

[{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"}}]
String value = arrayObject.get(VALUE).toString(); 
JSONArray array = new JSONArray(value); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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