首页新闻找找看学习计划

获取bosonnlp 新闻摘要 报错A JSONArray text must start with '[' at 1 [character 2 line 1]

0
悬赏园豆:100 [待解决问题]


public static void xwzy(){
String SENTIMENT_URL = "http://api.bosonnlp.com/summary/analysis";
String body = new JSONArray(new String[]{"(" +
"腾讯科技讯(刘亚澜)10月22日消息," +
"前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV,出任CTO一职。" +
"资料显示,黄冬历任土豆网技术副总裁、优酷土豆集团产品技术副总裁等职务," +
")"}).toString();
System.out.println(body);
HttpResponse<JsonNode> jsonResponse = null;
try {
jsonResponse = Unirest.post(SENTIMENT_URL)
.header("Accept", "application/json")
.header("X-Token", "P14_zfuc.15042.M-YcvmN7FtKm")
.body(body).asJson();


Unirest.shutdown();
} catch (UnirestException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println(jsonResponse.getBody());
}

public static void main(String[] args){
//gjc();
xwzy();

 

-------------------------------

 

["(腾讯科技讯(刘亚澜)10月22日消息,前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV,出任CTO一职。资料显示,黄冬历任土豆网技术副总裁、优酷土豆集团产品技术副总裁等职务,)"]
com.mashape.unirest.http.exceptions.UnirestException: java.lang.RuntimeException: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:145)
at com.mashape.unirest.request.BaseRequest.asJson(BaseRequest.java:68)
at net.bosondata.sentiment_api_example.SentimentApiExample.xwzy(SentimentApiExample.java:55)
at net.bosondata.sentiment_api_example.SentimentApiExample.main(SentimentApiExample.java:70)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at com.mashape.unirest.http.HttpResponse.<init>(HttpResponse.java:98)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:141)
... 3 more
Caused by: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at com.mashape.unirest.http.JsonNode.<init>(JsonNode.java:51)
at com.mashape.unirest.http.HttpResponse.<init>(HttpResponse.java:89)
... 4 more
Caused by: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONArray.<init>(JSONArray.java:105)
at org.json.JSONArray.<init>(JSONArray.java:144)
at com.mashape.unirest.http.JsonNode.<init>(JsonNode.java:48)
... 5 more
Exception in thread "main" java.lang.NullPointerException
at net.bosondata.sentiment_api_example.SentimentApiExample.xwzy(SentimentApiExample.java:65)
at net.bosondata.sentiment_api_example.SentimentApiExample.main(SentimentApiExample.java:70)

国强则无惧!的主页 国强则无惧! | 初学一级 | 园豆:102
提问于:2017-05-15 19:10
< >
分享
所有回答(3)
0

字符串不是json字符串

吴瑞祥 | 园豆:28731 (高人七级) | 2017-05-15 19:29

都试了 还是不行

 

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:03

@国强则无惧!: ...看了你下面发的.你那叫试过了..看样子你是不明白json格式标准的.

百度下吧.

支持(0) 反对(0) 吴瑞祥 | 园豆:28731 (高人七级) | 2017-05-16 10:14

{'content':'腾讯科技讯(刘亚澜)10月22日消息,
前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV,出任CTO一职。
资料显示,黄冬历任土豆网技术副总裁、优酷土豆集团产品技术副总裁等职务,'}
return:{"message":"decode: expected object key string but found invalid token at character 2","status":400}

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:17

@吴瑞祥: 我主要是调用BonsonNLP 这个接口 

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:19

@国强则无惧!: 这个字符串你要你反序列化成什么类型?

支持(0) 反对(0) 吴瑞祥 | 园豆:28731 (高人七级) | 2017-05-16 10:20

@吴瑞祥: json啊

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:21

@吴瑞祥: 

[[[0.5686631749811326,"蔓延"],[0.5671956747680966,"病毒"],[0.33993439662135194,"迅速"],[0.30642011458238383,"网站"],[0.26392731183346607,"新闻"],[0.23807884315568398,"媒体"],[0.12876489756725826,"式"],[0.0504282185215189,"让"]]]

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:21

@吴瑞祥: public static void gjc(){
String SENTIMENT_URL =
"http://api.bosonnlp.com/keywords/analysis";
String body = new JSONArray(new String[]{"病毒式媒体网站:让新闻迅速蔓延"}).toString();
HttpResponse<JsonNode> jsonResponse = null;
try {
jsonResponse = Unirest.post(SENTIMENT_URL)
.header("Accept", "application/json")
.header("X-Token", "P14_zfuc.15042.M-YcvmN7FtKm")
.body(body)
.asJson();
Unirest.shutdown();
} catch (UnirestException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println(jsonResponse.getBody());

}

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:21

@吴瑞祥: 这个就成功调用了

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:22

@国强则无惧!: 那我不知道你的问题是啥.

支持(0) 反对(0) 吴瑞祥 | 园豆:28731 (高人七级) | 2017-05-16 10:27

@吴瑞祥:

["腾讯科技讯(刘亚澜)10月22日消息,\n前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV,出任CTO一职。\n资料显示,黄冬历任土豆网技术副总裁、优酷土豆集团产品技术副总裁等职务,"]

 A JSONArray text must start with '[' at 1 [character 2 line 1]

 

问题就是这个 

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:28
0

错误信息很明确,要“[”开头

狼爷 | 园豆:1192 (小虾三级) | 2017-05-15 19:31

["['腾讯科技讯(刘亚澜)10月22日消息,\n''前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV,出任CTO一职。\n''资料显示,黄冬历任土豆网技术副总裁、优酷土豆集团产品技术副总裁等职务,']"]
{"message":"decode: expected the end but found invalid token at character 3","status":400}

 

这样也报错

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-16 10:06

@国强则无惧!: 格式要这样的 [{"腾讯":""腾讯科技讯(刘亚澜)10月22日消息。。。。"},{"资料显示":"黄冬历。。。"}]

支持(0) 反对(0) 狼爷 | 园豆:1192 (小虾三级) | 2017-05-16 18:50
0

哎,“ A JSONArray text must start with '[' at 1 [character 2 line 1]”错误提示已经告诉了你一切。

你的字符串必须拼接成JSON数组要求的格式。像这样:

[{"prop1":"value1" , "prop2":"value2" , ......},{},......]

这种格式的字符串才符合JSONArray构造器的要求。

西漠以西 | 园豆:1670 (小虾三级) | 2017-05-17 16:21

 但是 我写成这样 还是不行  

[{"not_exceed":0.2,"percentage":0,"title":"","content":"前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV出任CTO一职"}]
return:null
com.mashape.unirest.http.exceptions.UnirestException: java.lang.RuntimeException: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-17 16:24

你看看 我哪里写错了   

我试了 好几种 转json的 

public static void xwzy(){
String SENTIMENT_URL = "http://api.bosonnlp.com/summary/analysis";
//String[] b=new String[]{"前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV出任CTO一职"};
String b1="[{'not_exceed':0.2," +
"'percentage':0," +
"'title':''," +
"'content':'前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV出任CTO一职'}]";
String jsonMessage = "{\"not_exceed\":0.2," +
"\"percentage\":0," +
"\"title\":\"\"," +
"\"content\":\"前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV出任CTO一职\"}";
String[] arr = {"0","0.2","","2222222"};

String jsonarray =new JSONArray(arr).toString();
//System.out.println(jsonarray);
String body1 = new JSONObject(jsonMessage).toString();
String body = new JSONArray(b1).toString();



System.out.println(body);
HttpResponse<JsonNode> jsonResponse = null;

try {
jsonResponse = Unirest.post(SENTIMENT_URL)
.header("Accept", "application/json")
.header("X-Token", "P14_zfuc.15042.M-YcvmN7FtKm")
.body(body).asJson();
} catch (UnirestException e) {
e.printStackTrace();
}

System.out.println("return:"+jsonResponse);

}

支持(0) 反对(0) 国强则无惧! | 园豆:102 (初学一级) | 2017-05-17 16:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册