无法使用java将yaml字符串转换为json字符串

时间:2015-06-18 09:00:24

标签: java json rest yaml

我想使用" jyaml"将yaml字符串转换为json字符串,请帮助我解决此问题

YAML字符串: -

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

我的预期输出: -

{
  "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代码: -

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

输出我得到: -

"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"

我的替代yaml输入文件: -

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

当我尝试上传具有多个值的单个键时,例如&#34; user2:ch4,ch5,ch6&#34;。对于这种情况,我的代码不起作用。我得到了这个例外 :

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

2 个答案:

答案 0 :(得分:2)

缩进对于yaml文件至关重要。如果您的yaml文件与问题中的相同,则输出完全正确。为了实现预期的json,首先必须修复yaml文件。

为了获得您期望的json输出,您的yaml文件应如下所示。

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"

答案 1 :(得分:1)

获得snakeyaml jar http://www.java2s.com/Code/Jar/s/Downloadsnakeyaml15sourcesjar.htm 它可能适合你。这对我有用。