2017-10-16 31 views
-1

サードパーティAPIからJSONとして応答を受け取るJavaアプリケーションを作成しています。次のようにJSONは次のようになります。JSONエラーを解析するGson?

{"simulation": true, 
"transaction": { 
"time": "2015-09-08 17:58:58", 
"id": "123-123-123-123", 
"state": "1", 
"error": "Some error text, e.g. Incorrect mobile number", 
"service": { 
"id": "du-mt", 
"name": "Du More Time" 
}, 
"account": "971550000000", 
"amount": "5", 
"amount_currency": "AED", 
"price": "4.85", 
"price_currency": "AED" 
}, 
"info": 
{"reference": "1234567890", 
"number": "123412341234", 
"serial": "1234567890" 
}, 
"receipt": { 
"header": "Du", 
"subheader": "More Time", 
"info": "Merchant: GULFBOX, Terminal ID: 123", 
"instruction": "User instruction: ...", 
"promo": "", 
"footer":"Customer Service: 800 4269" 
} 
} 

は私が

public class GulfBox { 
    public String simulation; 
    public Transaction transactions; 
    public Info info; 
    public Receipt receipt; 
    public String getSimulation() { 
     return simulation; 
    } 
} 
public class Transaction { 
public String time; 
public String id; 
public String state; 
public String error; 
public String account; 
public String amount; 
public Service service; 
public String amount_currency; 
public String price; 
public String price_currency; 
public String getTime() { 
    return time; 
} 
public String getTransactionID() { 
    return id; 
} 
public String getState() { 
    return state; 
} 
public String getError() { 
    return error; 
} 
public String getAccount() { 
    return account; 
} 
public String getAmount() { 
    return amount; 
} 
public String getAmountCurrency() { 
    return amount_currency; 
} 
public String getPrice() { 
    return price; 
} 
public String getPriceCurrency() { 
    return price_currency; 
} 

} 
public class Service { 
public String id; 
public String name; 

public String getServiceId() { 
    return id; 
} 

public String getServiceName() { 
    return name; 
} 
} 

のようないくつかのゲッタークラスを作成し、私は実現される機能を呼び出すことを試みた:私はGET最初シミュレーションのための

String json = "{\"simulation\": true,\"transaction\": {\"time\": \"2015-09-08 17:58:58\",\"id\": \"123-123-123-123\",\"state\": \"1\",\"error\": \"Some error text, e.g. Incorrect mobile number\",\"service\": {\"id\": \"du-mt\",\"name\": \"Du More Time\"},\"account\": \"971550000000\",\"amount\": \"5\",\"amount_currency\": \"AED\",\"price\": \"4.85\",\"price_currency\": \"AED\"},\"info\": {\"reference\": \"1234567890\",\"number\": \"123412341234\",\"serial\": \"1234567890\"},\"receipt\": {\"header\": \"Du\",\"subheader\": \"More Time\",\"info\": \"Merchant: GULFBOX, Terminal ID: 123\",\"instruction\": \"User instruction: ...\",\"promo\": \"\",\"footer\":\"Customer Service: 800 4269\"}}"; 
      System.out.println(json); 
      GulfBox details= new Gson().fromJson(json, GulfBox.class); 
       transactionsID = "Success"; 
       System.out.println("SIMULATION : "+details.getSimulation().toString()); //Check If simulation true or False 

       System.out.println("transaction : "+details.transactions.getTime());//Transaction time 

を正しい値、、しかし、トランザクション時間はnull pinter例外です!任意のヘルプここに

+1

JSONが有効ですか?あなたのJSONが有効なJSONであることを確認する必要があるGsonやその他の解析テクニックのために、 "}"の直前にコンマがあるので、フォーマットが奇妙です。 –

+0

あなたの失敗したコードを表示し、失敗したことを教えてください – mlecz

+0

あなたの指定されたJSONが無効です。 JSON文字列を再確認してください –

答えて

-1

Gson APIは完全に動作するはずです。私はそれを長い間使用しています。以下のような

class AnyClassName{ 

private String simulation; 
private Transaction transaction;//Transaction should be another class 
private String account; 
private String amount; 
private String amount_currency; 
private String price; 
private String price_currency; 
private Info info; //Info should be another class 
private Receipt receipt;//Receipt should be another class 

//generate setters and getters for all above fields 

} 

使用GsonのAPI:

Gson gson = new Gson(); 
AnyClassName obj = new AnyClassName(); 

     // Java object to JSON, and assign to a String 

     String jsonInString = gson.toJson(obj); 

     //json string to java object 

     AnyClassName obj2= gson.fromJson(jsonInString, AnyClassName .class); 

あなたは注釈@produceを使用する場合は、コントローラからのUIに文字列を返す場合は、あなたのJSON形式ごとなどのJavaコードは以下のようにする必要があり ( 'application/json')またはUIコードでJSON.parseを使用してjson文字列をjsonオブジェクトに変換します。