繁体   English   中英

如何将String值转换为json?

[英]How to convert the String value into 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"}}]
String value = arrayObject.get(VALUE).toString(); 
JSONArray array = new JSONArray(value); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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