0

I'm trying to write a JSon document and its relative xsd file in order to generate JAXB classes and I don't think if I'm doing it in the right way. what I want is a structure like this:

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]

so I wrote my xsd and a example string in the json file to attempt to read it

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

and this is my 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>

eclipse keeps telling me that I have a MalformedJson, i think the problem is with the json line, i think that the xsd is almost correct, but i could be wrong :)

The code that writes the JSON document looks like this:

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)
{ 
} 

Which produces this JSON document:

{
  "nome":"team1",
  "game":"game",
  "intarray":[1,2],
  "arrayofintarrays":{
    "couple":[]
  },
  ‌​"arrayofstringarrays":{
    "mossearray":[]
  }
}

This JSON document is then read with like this:

Team[] teams_loaded= null; 

try
{ 
  Reader reader = new InputStreamReader(DexLoader.class.getClassLoader().getResourceAsStream("teams.tx‌​t")); 
  Gson gson = new GsonBuilder().create(); 
  JsonReader read = new JsonReader(reader); 
  teams_loaded = gson.fromJson(read, Team[].class); 
}
catch(Exception c)
{ 
  c.printStackTrace(); 
}

thanks!

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
user3252485
  • 65
  • 1
  • 10
  • As an aside, wouldn't it be simplier to just write the Java code rather than the XSD and then generate the Java code from it? – Nick Holt Jan 30 '14 at 10:07
  • I'm new on JSon, the only attempt I made for reading a JSon file was from a ready and well-made json document, I was able to quickly generate classes and using them on eclipse, so I thought this was the best way to proceed – user3252485 Jan 30 '14 at 10:09
  • The JSON is fine, I can parse it using Java JSON API (javax.json:javax.json-api:1.0 and org.glassfish:javax.json:1.0.4). What API are you using and what's the exception (including the stacktrace) that you are seeing? My guess is that you're using GSON API and that the classes generated from your XSD don't match the JSON. – Nick Holt Jan 30 '14 at 10:24
  • you are right, I'm using GSON API, I tried, like Nick Holt said, to just fill an object generated by JAXB and make GSON write it in a file, I got a json entry that looks fine, but when I try to read it back (using the same writing classes) eclipse gives me the MalformedJson exeption again! – user3252485 Jan 30 '14 at 10:38
  • Please add the code that's writing the object out and trying to read it back in. – Nick Holt Jan 30 '14 at 10:42
  • this is what I do for write //fill the object team 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){ } and this is what i get: {"nome":"team1","game":"game","intarray":[1,2],"arrayofintarrays":{"couple":[]},"arrayofstringarrays":{"mossearray":[]}} – user3252485 Jan 30 '14 at 10:46
  • this is the reading code: Team[] teams_loaded= null; try{ System.out.println(DexLoader.class.getClassLoader().getResource("teams.txt")); 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(); } and it gives me the error MalformedJson (don't mind about the different file pointing, i'm in debug) – user3252485 Jan 30 '14 at 10:49
  • possible duplicate of [Parsing JSON array with gson](http://stackoverflow.com/questions/9853017/parsing-json-array-with-gson) – Nick Holt Jan 30 '14 at 13:17

2 Answers2

0

You are writing a single instance of Team and then trying to read it as an array of Team. Your read code should be:

Team team = gson.fromJson(read, Team.class); 
Nick Holt
  • 33,455
  • 4
  • 52
  • 58
0

It seems that working with array JSON elements is a little tricky in GSON. Instead of reading your file as given like this

Team[] teams_loaded= null    
teams_loaded = gson.fromJson(read, Team[].class)

try to read an Array of teams as given below.

 @Test
    public void testSampleGSON() {
        String sample = "[{\"name\":\"Sharath\",\"age\":\"24\"},{\"name\":\"Sharath\",\"age\":\"24\"},{\"name\":\"Sharath\",\"age\":\"24\"},]";
        System.out.println(sample);

        Gson gson = new Gson();
        Type t = new TypeToken<Person[]>() {
        }.getType();
        Person[] pl = gson.fromJson(sample, t);
        System.out.println(pl);

        for (int i = 0; i < pl.length; i++) {
            System.out.println(pl[i]);
        }
        String jsonString = gson.toJson(pl);
        System.out.println(jsonString);
    }

Class Person is a Public Class in the same package as given below.

public class Person {
    String name;
    String age;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public String getAge() {
        return this.age;
    }

    public void setAge(String age) {
        this.age = age;
    }

Hope this helps.

    public String toString() {
        return "Name : " + name + " Age : " + age;
    }
}
Sharath Bhaskara
  • 467
  • 6
  • 12
  • In the above code, Type t = new TypeToken() {}.getType(); Person[] pl = gson.fromJson(sample, t); Will help you to retain the Type information at runtime. – Sharath Bhaskara Jan 31 '14 at 13:30