Reputation: 647
I want to convert a yaml
string to a json
string by using "jyaml", kindly some one help me to resolve this
YAML string :-
Korea: www.gozoculture.com: default: jhfjfjfj www.5plus5.gov.mt: default: jhfjfjfj www.ces.gov.mt: default: cesDefault user5: chennal5 isp: Tata DoCoMo: chennalisp1 Hathway: chennalIsp Hong Kong: chennalisp2 Aircel: chennalIspaircel Airtel: chennalIspAirtel studiodefault: KoreaStudioDefault www.wwe.com: default: wweDefault user2: chennal2 user1: chennal1 country: FR: francecountryChennal HK: HongkongCountryChennal IN: IndaiChennal DE: GermanyCountryChennal
my expected output:-
{
"Singapoor": {
"studiodefault": "chennal default singapoor site",
"www.gozoculture.com": {
"default": "chennalDefault1",
"user1": "ch1,ch2,ch3"
},
"country": {
"FR": "franceChennal",
"DE": "GermanyChennal",
"IN": "indiaChennal"
},
"www.justice.gov.mt": {
"default": "justiceDefault"
},
"www.wwe.com": {
"default": "wwechennalDefault",
"user2": "ch4,ch5,ch6"
},
"isp": {
"Vodafone Broadband": "vodafoneChennal2",
"Idea cellular": "ideaChennal",
"Airtel": "aritelChennal1"
},
"www.enemalta.com": {
"default": "chennalDefault2",
"user3": "ch7,ch8,ch9"
}
}
}
java code:-
private static String convertToJson(String yamlString) {
Yaml yaml= new Yaml();
Map<String,Object> map= (Map<String, Object>) yaml.load(yamlString);
JSONObject jsonObject=new JSONObject(map);
return jsonObject.toString();
}
output I am getting :-
"Korea": "www.gozoculture.com: default: jhfjfjfj www.5plus5.gov.mt: default: jhfjfjfj www.ces.gov.mt: default: cesDefault user5: chennal5 isp: Tata DoCoMo: chennalisp1 Hathway: chennalIsp Hong Kong: chennalisp2 Aircel: chennalIspaircel Airtel: chennalIspAirtel studiodefault: KoreaStudioDefault www.wwe.com: default: wweDefault user2: chennal2 user1: chennal1 country: FR: francecountryChennal HK: HongkongCountryChennal IN: IndaiChennal DE: GermanyCountryChennal"
my alternative yaml input file:-
Singapoor:
www.gozoculture.com:
default: chennalDefault1
user1: ch1,ch2,ch3
isp:
Airtel: aritelChennal1
Vodafone Broadband: vodafoneChennal2
Idea cellular: ideaChennal
www.enemalta.com:
default: chennalDefault2
user3: ch7,ch8,ch9
www.justice.gov.mt:
default: justiceDefault
studiodefault: chennal default singapoor site
www.wwe.com:
default: wwechennalDefault
user2: ch4,ch5,ch6
country:
FR: franceChennal
IN: indiaChennal
DE: GermanyChennal
when I trying to upload single key with multiple values like "user2: ch4,ch5,ch6". for this case my code does not work. I am getting this exception :
Jun 18, 2015 5:29:23 PM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: org.ho.yaml.exception.YamlException: Error near line 4: Unterminated inline value
Upvotes: 2
Views: 1999
Reputation: 335
get snakeyaml jar http://www.java2s.com/Code/Jar/s/Downloadsnakeyaml15sourcesjar.htm it may be work for you.it is work for me.
Upvotes: 1
Reputation: 6702
Indentation is crucial for yaml files. If your yaml file as same as in the question, the output is totally correct. In order to achieve expected json, first you must fix your yaml file.
In order to get your expected json output your yaml file should look like this.
Singapoor:
studiodefault: "chennal default singapoor site"
www.gozoculture.com:
default: "chennalDefault1"
user1: "ch1,ch2,ch3"
country:
FR: "franceChennal"
DE: "GermanyChennal"
IN: "indiaChennal"
www.justice.gov.mt:
default: "justiceDefault"
www.wwe.com:
default: "wwechennalDefault"
user2: "ch4,ch5,ch6"
isp:
Vodafone Broadband: "vodafoneChennal2"
Idea cellular: "ideaChennal"
Airtel: "aritelChennal1"
www.enemalta.com:
default: "chennalDefault2"
user3: "ch7,ch8,ch9"
Upvotes: 2