尝试解析json时出错 - 遇到意外字符

时间:2014-04-09 10:55:54

标签: android json parsing

这是我需要解析的json格式

{
"transaction":
{
  "amount":{
    "total":"122",  
    "currencyCode":"GPP"  
  },
  "qrcode" : "1f0e3dad99908345f7439f8ffabdffop",
  "description": "This is the payment transaction description."
}
}

当我解析这个时,我得到了这个错误 enter image description here

这就是我解析数据的方式

StringEntity params = new StringEntity("{" +
                                                  "transaction : "+
                                                    "{"+
                                                      "amount : {" +
                                                        "total : " + amount + ","+  
                                                        "currencyCode : " + currency +  
                                                      "},"+ 
                                                      "qrcode : " + key + "," + 
                                                      "description : " + "This is the payment transaction description." +
                                                    "}" );
request.addHeader(AppConstants.PAYMENT_HEADER1, BEARER);
request.addHeader(AppConstants.content_type, AppConstants.application_json);
    request.setEntity(params);
  try {
      response = httpClient.execute(request);
  entity = response.getEntity();

  inputStream = entity.getContent();                 
  BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
  sb.append(line + "\n");
 }
result = sb.toString();
Log.e("RESULT", result);
    }catch (Exception ex) {

 Log.e("RESULT_ERROR", ex.toString());
 }

让anyopne在此之前解决了这个问题.... ??

提前致谢

解决

感谢SUSHANT和OPAKASH

我需要格式化我的字符串json,我也错过了一个cuurly大括号 最终字符串是

"{" +
    "transaction : "+
    "{"+
    "amount : {" +
    "total : \"" + amount + "\","+ 
    "currencyCode : \"" + currency +  
    "\"},"+ 
    "qrcode : \"" + key + "\"," + 
    "description : " + "\"This is the payment transaction description.\"" +
"}}" 

4 个答案:

答案 0 :(得分:3)

在java中构建json字符串时,你必须转义双引号。

您的情况是正确的:

new StringEntity("{" +
   "transaction : "+
   "{"+
   "amount : {" +
   "total : \"" + amount + "\","+ 
   "currencyCode : \"" + currency +  
   "\"},"+ 
   "qrcode : \"" + key + "\"," + 
   "description : " + "\"This is the payment transaction description.\"" +
"}" );

答案 1 :(得分:1)

您必须在所有ID json中添加“”(必须使用'/'

进行转义
StringEntity params = new StringEntity("{" +
                                              "\"transaction\" :"+ //and so on

这使得params的内容

{"transaction": //...

而不是

{transaction: //...

答案 2 :(得分:1)

我想你错过了一个结束'}'支架。

StringEntity params = new StringEntity("{" + "\"transaction\" : " + "{"+ "\"amount\" : {" + "\"total\" : " + amount + ","+ "\"currencyCode\" : " + currency + "},"+ "\"qrcode\" : " + key + "," + "\"description\" : " + "This is the payment transaction description." + "}}" );

答案 3 :(得分:0)

似乎发生这种情况是因为您的请求JSON字符串未正确构建。你必须把你的键和值用双引号(之前使用\转义):

 StringEntity params = new StringEntity("{" + "\"transaction\" : " + "{" + "\"amount\" : {" + "\"total\" : " + "\"" + amount + "\"" + "," + "\"currencyCode\" : " + "\"" + currency + "\" + "}," + "\"qrcode\" : " + "\"" + key + "\"" + "," + "\"description\" : "
        + "\"This is the payment transaction description.\"" + "}");