2017-03-23 2 views
0

Jsonファイルからオブジェクトを作成する際に問題が発生しています。私は3つのクラス、オブジェクトの作成を処理するGsonReader、1つはPOJOクラスのModelとGsonReaderからメソッドを呼び出すMainメソッドクラスです。何が間違っているのか教えてください。JSONファイルからオブジェクトを作成しようとするとMalformedJsonExceptionが発生する

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()); 
} 
} 

メイン

import java.io.FileNotFoundException; 

public class Main { 

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

} 

} 

モデル

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' 
} 
+1

jsonは秘密ですか? –

+0

私はそれについて忘れてしまった、今追加しました:D – Tudor

+0

あなたの引用符がおそらく理由です。 '' 'を使用します。また、' example'は文字列か数値ですか?それが文字列の場合も同様です。 – Justas

答えて

0

あなたのJSONは、カンマで区切る属性および適切な引用符を使用します。すべての周りの

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

使用標準の引用符とカンマ: -

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

これは今http://json.parser.online.fr/に従って検証します。

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

JSONが間違っています。

文字列プロパティのためにそれがなければなりません。また

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

、私はGsonに精通していないんだけど設定することによって:

setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) 

それはようであるべきか?

{ 
    "Name": "example", 
    "Type": "example", 
    "Value": "example" 
} 
関連する問題