我想寫一個JSon文檔及其相關的xsd文件,以生成JAXB類,我不認爲如果我以正確的方式做它。 我想是這樣的結構:JSon數組數組
team
-name="name"
-game="game"
-intarray
int
int
...
int
-values
[int1, int2]
[int1, int2]
...
[int1, int2]
-stringarrays
[string1, string2,...., stringn]
[string1, string2,...., stringn]
...
[string1, string2,...., stringn]
所以我寫了我的XSD和在JSON文件中的示例串試圖讀取它
example line:
{"name": "Team 1", "game": "game", "intarray": [1, 2, 3, 4, 5, 6], "values": [[10,20], [10,80], [10,30], [10,60], [10,50], [10,30]], "stringarrays": [["Azione", "Azione"], ["Azione", "Azione"], ["Azione", "Azione"], ["Azione", "Azione"], ["Azione", "Azione"], ["Azione", "Azione"]]}
,這是我的xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/teams" xmlns:tns="http://www.example.org/teams" elementFormDefault="qualified">
<element name="team_list" type="tns:lista_team"></element>
<complexType name="team">
<sequence>
<element name="name" type="string"></element>
<element name="game" type="string"></element>
<element name="intarray" type="int" maxOccurs="unbounded"
minOccurs="0">
</element>
<element name="values" type="tns:ivev"></element>
<element name="stringarrays" type="tns:arrays"></element>
</sequence>
</complexType>
<complexType name="ivev">
<sequence>
<element name="couple" type="tns:ivev_couple" maxOccurs="unbounded" minOccurs="0"></element>
</sequence>
</complexType>
<complexType name="ivev_couple">
<sequence>
<element name="ivev_element" type="int" maxOccurs="unbounded" minOccurs="1"></element>
</sequence>
</complexType>
<complexType name="moveset">
<sequence>
<element name="mosse" type="string" maxOccurs="unbounded" minOccurs="0"></element>
</sequence>
</complexType>
<complexType name="arrays">
<sequence>
<element name="stringarray" type="tns:moveset" maxOccurs="unbounded" minOccurs="0"></element>
</sequence>
</complexType>
<complexType name="lista_team">
<sequence>
<element name="squadra" type="tns:team" maxOccurs="unbounded" minOccurs="0"></element>
</sequence>
</complexType>
日食總是告訴我,我有一個MalformedJson,我THI NK問題是使用JSON行,我認爲XSD幾乎是正確的,但我可能是錯的:)
寫入的JSON文檔看起來像這樣的代碼:
Gson gson = new Gson();
String json = gson.toJson(team);
try
{
FileWriter gwriter = new FileWriter("c:\\file.json");
gwriter.write(json);
gwriter.close();
}
catch(Exception e)
{
}
將會產生這JSON文件:
{
"nome":"team1",
"game":"game",
"intarray":[1,2],
"arrayofintarrays":{
"couple":[]
},
"arrayofstringarrays":{
"mossearray":[]
}
}
這個JSON文件,然後讀取這樣的:
Team[] teams_loaded= null;
try
{
Reader reader = new InputStreamReader(DexLoader.class.getClassLoader().getResourceAsStream("teams.txt"));
Gson gson = new GsonBuilder().create();
JsonReader read = new JsonReader(reader);
teams_loaded = gson.fromJson(read, Team[].class);
}
catch(Exception c)
{
c.printStackTrace();
}
謝謝!
另外,僅僅編寫Java代碼而不是XSD,然後從中生成Java代碼不是簡單的嗎? –
我是JSon的新手,我爲閱讀JSon文件而做的唯一嘗試是從一個現成的,精心製作的json文檔中,我能夠快速生成類並在eclipse上使用它們,所以我認爲這是最好的方法繼續 – user3252485
JSON很好,我可以使用Java JSON API(javax.json:javax.json-api:1.0和org.glassfish:javax.json:1.0.4)解析它。你使用的是什麼API,以及你所看到的異常(包括堆棧跟蹤)是什麼?我的猜測是你使用的是GSON API,並且你的XSD生成的類與JSON不匹配。 –