2

have a structure for a contact book:

Contact { "Name":{ "First name":"Erm","Last Name": "Smith" }, "Number":67485647 }.

I save it as a Json object and than store it in a file with createContact().

public synchronized void createContact (String fname, String lname,String typeof, int number) throws FileNotFoundException 
{   
    JsonObjectBuilder contactBuilder = Json.createObjectBuilder();
    JsonObjectBuilder nameBuilder = Json.createObjectBuilder();
    JsonArrayBuilder  contactArrayBuilder= Json.createArrayBuilder();

    nameBuilder.add("First Name",fname);
    nameBuilder.add("Last Name",lname);
    contactBuilder.add("Name", nameBuilder);
    contactBuilder.add("Type", typeof);
    contactBuilder.add("number", number);

    JsonObject contact = contactBuilder.build();
    contactArrayBuilder.add(contact);
    JsonArray contactArray=contactArrayBuilder.build();

    //write to file
    OutputStream os = new FileOutputStream(filename);
    JsonWriter fileWriter = Json.createWriter(os);
    fileWriter.writeArray(contactArray);
    fileWriter.close();
}

I read the file with:

InputStream is = new FileInputStream(filename);
JsonReader fileReader = Json.createReader(is);
JsonArray contactObj = (JsonArray) Json.createParserFactory(fileReader.readObject());
JsonArrayBuilder contactArrayBuilder= Json.createArrayBuilder();

This is when I get the error

javax.json.JsonException: Cannot read JSON object, found JSON array
at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:105)

I am using javax.json

2 Answers 2

1

The problem is that you are storing a JsonArray in that file, and then trying to read that JsonArray as a JsonObject. What you can do to get your code to work is remove these lines:

JsonArray contactObj = (JsonArray) Json.createParserFactory(fileReader.readArray());
JsonArrayBuilder contactArrayBuilder= Json.createArrayBuilder();

and replace them with something like this:

JsonArray array = fileReader.readArray();

In the end, reading the file will look like this:

InputStream is = new FileInputStream(filename);
JsonReader fileReader = Json.createReader(is);
JsonArray array = fileReader.readArray();
Sign up to request clarification or add additional context in comments.

Comments

0

Change this line

JsonArray contactObj = (JsonArray) Json.createParserFactory(fileReader.readObject());

to this

JsonArray contactObj = (JsonArray) Json.createParserFactory(fileReader.readArray());

as you are expecting an array but reading the object you are getting this error.

3 Comments

If I do it like you suggest I get an error because createParserFactory does not take an array argument. If I do it like this: JsonArray contactObj = (JsonArray) Json.createParser((Reader) fileReader.readArray()); This dos not give an error, but is it a correct way to do it?
No I get the same error if I do that in the above comment
You can just do JsonArray contactObj = fileReader.readArray(); and it should work I think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.