2017-10-16 109 views
-1

我正在创建一个Java应用程序,它可以从第三方API获取作为JSON的响应。该JSON将作如下安排:解析JSON错误usig 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; 
} 
} 

并试图调用一个函数在那里我实现:

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 

对于第一模拟我得到正确的价值,,但对于交易时间它的空pinter异常!任何帮助在这里

+1

您确定JSON有效吗?我的意思是这种格式很奇怪,因为你在使用Gson或其他解析技术时“}”之前有一个逗号,所以你应该确保你的JSON是一个有效的JSON。 –

+0

告诉我们你的失败代码,并告诉你失败 – mlecz

+0

你提供的JSON无效。重新检查JSON字符串 –

回答

-1

Gson API应该完美的工作,我使用它很长一段时间。 按照您的JSON格式的Java代码应该象下面这样:

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 ('application/json')或者在UI代码中使用JSON.parse将json字符串转换为json对象。