Fastjson解析json時遇到問題之后——


今天,2015年10月29日。

在運行eclipse時,遇到如下問題:

1  com.alibaba.fastjson.JSONException: syntax error, expect {, actual error, pos 0
2  at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:212)
3  at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseRest(ASMJavaBeanDeserializer.java:96)
4  at Fastjson_ASM_UpdateRequest_1.deserialze(Unknown Source)
5  at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:513)
6  at com.alibaba.fastjson.JSON.parseObject(JSON.java:244)
7  at com.alibaba.fastjson.JSON.parseObject(JSON.java:220)
8  at com.alibaba.fastjson.JSON.parseObject(JSON.java:179)
9  at com.alibaba.fastjson.JSON.parseObject(JSON.java:327)

 

看到這個,第一反應是解析的json格式不對,遂詳細排查json格式問題。無奈怎么也找不到原因,在網上也沒有找到解決方案。后無意間發現,原來是http請求方式錯誤。Get請求寫成了Post請求。因此記錄下這次的錯誤,以后不要再犯。

另附http請求之POSTandGET。

  • POST篇:
 1 private static Token getRemoteAccessToken() {
 2         CloseableHttpClient httpClient = HttpClients.createDefault();
 3         HttpPost httppost = new HttpPost(URL);
 4         httppost.setHeader("Content-Type", ContentType);
 5         List<NameValuePair> params = new ArrayList<>();
 6         params.add(new BasicNameValuePair("grant_type", GrantTypeCredentials));
 7         params.add(new BasicNameValuePair("client_id", ClientId));
 8         params.add(new BasicNameValuePair("client_secret", ClientSecret));
 9         params.add(new BasicNameValuePair("scope", Scope));
10         UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, Charset.forName("UTF-8"));
11         
12         // 獲取Token。
13         Token token = null;
14         httppost.setEntity(urlEncodedFormEntity);
15         // 發送
16         try (CloseableHttpResponse httpResponse = httpClient.execute(httppost)) {
17             HttpEntity entity = httpResponse.getEntity();
18             if (entity != null) {
19                 String entityString = EntityUtils.toString(entity, Charset.forName("UTF-8"));
20                 token = JSON.parseObject(entityString, Token.class);
21             }
22         } catch (IOException e) {
23             LOGGER.error(ExceptionUtils.parseExceptionStackTrace(e));
24         } finally {
25             try {
26                 httpClient.close();
27             } catch (IOException e) {
28                 LOGGER.error(ExceptionUtils.parseExceptionStackTrace(e));
29             }
30         }
31         return token;
32     }

 

  • GET篇:
 1 public Double getValue(String from,String to,Double value,String url) {
 2         StringBuilder sbUri = new StringBuilder(url);
 3         sbUri.append("?");
 4         sbUri.append("from=");sbUri.append(from);
 5         sbUri.append("&");
 6         sbUri.append("to=");sbUri.append(to);
 7         sbUri.append("&");
 8         sbUri.append("value=");sbUri.append(value.toString());
 9         
10         //獲取轉換后value
11         CloseableHttpClient httpClient = HttpClients.createDefault();
12         HttpGet httpGet = new HttpGet(sbUri.toString()); 
13         httpGet.setHeader("Content-Type", ContentType);
14         
15         UnitValue unitValue = null;        
16         try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {        
17             HttpEntity entity = httpResponse.getEntity();
18             if (entity != null) {
19                 String entityString = EntityUtils.toString(entity, Charset.forName("UTF-8"));
20                 unitValue = JSON.parseObject(entityString, UnitValue.class);            
21                 value=unitValue.getValue();
22             }
23         } catch (IOException e) {
24             LOGGER.error(ExceptionUtils.parseExceptionStackTrace(e));
25         } finally {
26             try {
27                 httpClient.close();
28             } catch (IOException e) {
29                 LOGGER.error(ExceptionUtils.parseExceptionStackTrace(e));
30             }
31         }
32         return value;
33     }

 

                


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM