Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试分析json时出错-遇到意外字符_Android_Json_Parsing - Fatal编程技术网

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

Android 尝试分析json时出错-遇到意外字符,android,json,parsing,Android,Json,Parsing,这是我需要解析的json格式 { "transaction": { "amount":{ "total":"122", "currencyCode":"GPP" }, "qrcode" : "1f0e3dad99908345f7439f8ffabdffop", "description": "This is the payment transaction description." } } 当我解析这个时,我得到了这个错误 这就是我解析数据的方式 St

这是我需要解析的json格式

{
"transaction":
{
  "amount":{
    "total":"122",  
    "currencyCode":"GPP"  
  },
  "qrcode" : "1f0e3dad99908345f7439f8ffabdffop",
  "description": "This is the payment transaction description."
}
}
当我解析这个时,我得到了这个错误

这就是我解析数据的方式

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和PRAKASH

我需要格式化我的字符串json,我还遗漏了一个大括号 最后一个字符串是

"{" +
    "transaction : "+
    "{"+
    "amount : {" +
    "total : \"" + amount + "\","+ 
    "currencyCode : \"" + currency +  
    "\"},"+ 
    "qrcode : \"" + key + "\"," + 
    "description : " + "\"This is the payment transaction description.\"" +
"}}" 
必须将“”添加到所有IDs json(必须用“/”转义)

StringEntity params = new StringEntity("{" +
                                              "\"transaction\" :"+ //and so on
这使得参数的内容

{"transaction": //...
而不是

{transaction: //...

这似乎是因为您的请求JSON字符串没有正确构建。您必须将键和值用双引号括起来(以前用\)转义):


我想你错过了一个结束括号

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

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

正确的案例是:

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

键也应该有双引号吗?好问题:这取决于你拥有的数据类型,它是数字,不需要双引号。@PrakashBhagat可以在最后添加一个大括号,字符串格式帮助我解决了我缺少括号的问题。@PrakashBagat也给了我一个很好的例子。非常感谢你uch
new StringEntity("{" +
   "transaction : "+
   "{"+
   "amount : {" +
   "total : \"" + amount + "\","+ 
   "currencyCode : \"" + currency +  
   "\"},"+ 
   "qrcode : \"" + key + "\"," + 
   "description : " + "\"This is the payment transaction description.\"" +
"}" );