Tudor
Tudor

Reputation: 209

MalformedJsonException when trying to create an object from a JSON file

I am having trouble creating an object from a Json file. I have three classes, GsonReader that handles creating the object, one is the POJO class Model and the Main method class where i call the methods from GsonReader Can you please tell me what is wrong with my code, with some explanations?

EDITED

GsonReader

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;

public class GsonReader {

private String path = "D:\\ImportantStuff\\Validis\\Automation\\json.txt";

public void requestGson() throws FileNotFoundException {
    Gson gson = new GsonBuilder()
            .disableHtmlEscaping()
            .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
            .setPrettyPrinting()
            .serializeNulls()
            .create();
    JsonReader reader = new JsonReader(new FileReader(path));
    //BufferedReader reader = new BufferedReader(new FileReader(path));
    Object json = gson.fromJson(reader, Model.class);
    System.out.println(json.toString());
 }
}

Main

import java.io.FileNotFoundException;

public class Main {

public static void main(String[] args) throws FileNotFoundException {
    GsonReader r = new GsonReader();
    r.requestGson();

 }

}

Model

public class Model {
    private String name;
    private String type;
    private String value;

public Model(String name, String type, String value){
    this.name = name;
    this.type = type;
    this.value = value;
}

public String getName(){
    return name;
}

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

public String getType(){
    return type;
}

public void setType(String type){
    this.type = type;
}

public String getValue(){
    return value;
}

public void setValue(String value){
    this.value = value;
 }
}
public String toString(){
    return "Name: " + name + "\n" + "Type: " + type + "\n" + "Value: " + value;
}

Json

{
'name': 'Branding',
'type': 'String',
'value': 'Tester'
}

Upvotes: 0

Views: 376

Answers (3)

Kamesh
Kamesh

Reputation: 1152

Seperate your JSON attributes with comma and use appropriate Quotes.

{
"name": "example",
"type": "example",
"value": "example"
}

Upvotes: 0

Vadim
Vadim

Reputation: 4120

{
  “name”: example,
  “type”: example,
  “value”: example
}

Is exactly malformed JSON.

for string properties it must be:

{
   "name": "example",
   "type": "example",
   "value": "example"
}

Also, I'm not very familiar with Gson but by setting:

setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)

Should it be like?

 {
   "Name": "example",
   "Type": "example",
   "Value": "example"
 }

Upvotes: 0

Steve Smith
Steve Smith

Reputation: 2270

Use standard quotes and commas around everything:-

{
"name": "example",
"type": "example",
"value": "example"
}

This now validates according to http://json.parser.online.fr/ .

Upvotes: 3

Related Questions