2016-12-08 64 views
-2

YAML空間名稱是解析YAML用JAVA

innings: 
    - 1st innings: 
     team: Australia 
     runs:500 
    - 2nd innings: 
     team: Australia 
     runs:501 

Java代碼:

import java.io.File; 
import java.util.List; 

import org.apache.commons.lang3.builder.ReflectionToStringBuilder; 
import org.apache.commons.lang3.builder.ToStringStyle; 

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; 

/** 
* Hello world! 
* 
*/ 
public class App { 
    public static void main(String[] args) throws Exception { 
     ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); 
     try { 
      Innings user = mapper.readValue(new File("a.txt"), Innings.class); 
      System.out.println(ReflectionToStringBuilder.toString(user,ToStringStyle.MULTI_LINE_STYLE)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

class Innings { 
    public List<Inning> innings;  
} 
class Inning{ 
    public int runs; 
    public String team; 
} 

錯誤追蹤: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:無法識別的領域「第1局(inning)「,沒有標記爲可忽略的(2個已知屬性:,」runs「,」team「]) at [Source:a.txt;行:3,欄:7](通過參考鏈:局[[局]] - >局[[第一局]])

注意:請建議其他解析器,如果傑克遜無法幫助這裏。

+2

只要知道java開發人員在他們首先看到用小寫字母命名類的時候會感到非常痛苦。 – Scadge

+0

或非私人課程中的公共非終結字段。 – Qix

+0

是的,我明白了,其實我有同名的「圍墾」同一類,創建該類暫時我做了一個多級的「局」 – Nitsshukla

回答

0

得到它使用自定義解串器。謝謝大家。

import java.io.File; 
import java.io.IOException; 
import java.util.List; 

import org.apache.commons.lang3.builder.ReflectionToStringBuilder; 
import org.apache.commons.lang3.builder.ToStringStyle; 

import com.fasterxml.jackson.core.JsonParser; 
import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.DeserializationContext; 
import com.fasterxml.jackson.databind.JsonNode; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; 
import com.fasterxml.jackson.databind.module.SimpleModule; 
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; 

public class InningsParse { 
    public static void main(String[] args) { 
     ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); 
     SimpleModule module = new SimpleModule(); 
     module.addDeserializer(Inning.class, new InningDeserializer()); 
     mapper.registerModule(module); 
     try { 
      Innings user = mapper.readValue(new File("a.txt"), Innings.class); 
      System.out.println(ReflectionToStringBuilder.toString(user,ToStringStyle.MULTI_LINE_STYLE)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
class Innings { 
    public List<Inning> innings; 
} 
class Inning{ 
    public int runs; 
    public String team; 
} 
class InningDeserializer extends StdDeserializer<Inning> { 

    public InningDeserializer() { 
     this(null); 
    } 

    public InningDeserializer(Class<?> vc) { 
     super(vc); 
    } 

    @Override 
    public Inning deserialize(JsonParser jp, DeserializationContext ctxt) 
     throws IOException, JsonProcessingException { 
     JsonNode node = jp.getCodec().readTree(jp); 
     String nodeS = node.toString(); 
     nodeS = nodeS.substring(nodeS.indexOf(':')+1, nodeS.toString().lastIndexOf('}')); 
     System.out.println(nodeS); 
     ObjectMapper mapper = new ObjectMapper(); 
     return mapper.readValue(nodeS,Inning.class); 
    } 
} 
0

我不是超級熟悉的YAMLFactory但如果我修改這樣你的POJO:

class innings { 
    public inning[] innings; 
} 
class inning { 
    @JsonProperty("1st innings") 
    public inning_deep first; 

    @JsonProperty("2nd innings") 
    public inning_deep second; 
} 
class inning_deep{ 
    public int runs; 
    public String team; 
} 

它似乎反序列化就OK,但很明顯,這可能是不夠的,你。如果這是JSON,我會用數組替換兩個inning_deep屬性,它會工作得很好。我認爲這是YAMLFactory中的一個限制,您可能需要編寫一個自定義解串器。

你可能會打開一個問題here得到更多的幫助。 請重新考慮班級命名,以尊重naming conventions

注:通常我不會嘗試發佈一個不完整的答案,但它並不像其他任何人知道如何解決它。

+0

感謝很多答案。 是的,我想要一個沒有硬編碼的答案。 你能推薦一些其他解析器來解決這個問題嗎?即使它在Python,Ruby,.NET中也無關緊要。儘管在JAVA中 – Nitsshukla