Java 尝试从Json到字符串获取值时出现JSONException
我试图通过下一个API链接从Wikipedia获取2个值: 因为它是随机生成的,有时它不会返回我需要的一个值,但我稍后会解决这个问题,目前我在访问Json中需要的两个值“title”和“source”时遇到问题 返回的Json如下所示:Java 尝试从Json到字符串获取值时出现JSONException,java,json,Java,Json,我试图通过下一个API链接从Wikipedia获取2个值: 因为它是随机生成的,有时它不会返回我需要的一个值,但我稍后会解决这个问题,目前我在访问Json中需要的两个值“title”和“source”时遇到问题 返回的Json如下所示: {"batchcomplete":"","continue":{"grncontinue":"0.360395277951|0.360395626487|10429617|0","continue":"grncontinue||"},"query":{"pa
{"batchcomplete":"","continue":{"grncontinue":"0.360395277951|0.360395626487|10429617|0","continue":"grncontinue||"},"query":{"pages":{"38690716":{"pageid":38690716,"ns":0,"title":"Alaine Chartrand","thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/d/d4/Alaine_Chartrand.jpg","width":267,"height":400},"pageimage":"Alaine_Chartrand.jpg"}}}}
这就是代码,有人知道为什么它会转到JSONException吗
String API = "https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//open connection with wikipedia.
HttpURLConnection httpcon = (HttpURLConnection) new URL(API).openConnection();
//read all the input from wikipedia.
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String responseSB = in.lines().collect(Collectors.joining());
in.close();
JSONObject incomingJSON = new JSONObject(responseSB);
//crashes here
String mTitle = incomingJSON.getString("title");
String mUrl = incomingJSON.getString("source");
您无法直接从JSON响应获取标题和源代码,因为它必须包含多个内部对象。下面是阅读标题和源代码的代码快照
// new code
JSONObject incomingJSON = new JSONObject(responseSB);
JSONObject innerObject = incomingJSON.getJsonObject("query").getJsonObject("pages").getJsonObject("38690716");
String mTitle= innerObject.getString("title");
String mUrl= innerObject.getJsonObject("thumbnail").getString("source");
//crashes here
String mTitle = incomingJSON.getString("title");
String mUrl = incomingJSON.getString("source");
您无法直接从JSON响应获取标题和源代码,因为它必须包含多个内部对象。下面是阅读标题和源代码的代码快照
// new code
JSONObject incomingJSON = new JSONObject(responseSB);
JSONObject innerObject = incomingJSON.getJsonObject("query").getJsonObject("pages").getJsonObject("38690716");
String mTitle= innerObject.getString("title");
String mUrl= innerObject.getJsonObject("thumbnail").getString("source");
//crashes here
String mTitle = incomingJSON.getString("title");
String mUrl = incomingJSON.getString("source");
如果您注意到JSON,它是随机生成的,但具有特定的格式 案例1
{
"batchcomplete": "",
"continue": {
"grncontinue": "0.720220803439|0.720221273467|12887566|0",
"continue": "grncontinue||"
},
"query": {
"pages": {
"4897672": {
"pageid": 4897672,
"ns": 0,
"title": "New Hope, Sunnyvale, Texas"
}
}
}
}
query
和pages
始终存在,并且在页面中键总是随机生成的,因此它是Map
Map的String
键和JSONObject
作为值,然后需要从映射值中获取title
值
String API = "https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//open connection with wikipedia.
HttpURLConnection httpcon = (HttpURLConnection) new URL(API).openConnection();
//read all the input from wikipedia.
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String responseSB = in.lines().collect(Collectors.joining());
in.close();
JSONObject incomingJSON = new JSONObject(responseSB);
Map<String,JSONObject> map = (Map<String, JSONObject>) incomingJSON.getJSONObject("query").getJSONObject("pages");
map.forEach((k,v)->System.out.println(" The key is : "+k+" the title is : "+v.getString("title")));
因此,source
可能不会出现在每个响应中,请使用try-catch进行处理
String API = "https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//open connection with wikipedia.
HttpURLConnection httpcon = (HttpURLConnection) new URL(API).openConnection();
//read all the input from wikipedia.
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String responseSB = in.lines().collect(Collectors.joining());
in.close();
JSONObject incomingJSON = new JSONObject(responseSB);
Map<String,JSONObject> map = (Map<String, JSONObject>) incomingJSON.getJSONObject("query").getJSONObject("pages");
map.forEach((k,v)->{
System.out.println(" The key is : "+k+" the title is : "+v.getString("title"));
//use try catch to get source because you will not get the same response every time
String source = v.getJSONObject("thumbnail").getString("source");
});
}
stringapi=”https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//打开与维基百科的连接。
HttpURLConnection httpcon=(HttpURLConnection)新URL(API).openConnection();
//阅读维基百科的所有输入。
BufferedReader in=新的BufferedReader(新的InputStreamReader(httpcon.getInputStream());
字符串responseb=in.lines().collect(collector.joining());
in.close();
JSONObject incomingJSON=新的JSONObject(responseB);
Map Map=(Map)incomingJSON.getJSONObject(“查询”).getJSONObject(“页面”);
图.forEach((k,v)->{
System.out.println(“键为:+k+”,标题为:+v.getString(“标题”);
//使用try-catch获取源代码,因为您不会每次都得到相同的响应
字符串源=v.getJSONObject(“缩略图”).getString(“源”);
});
}
如果您注意到JSON,它是随机生成的,但具有特定的格式
案例1
{
"batchcomplete": "",
"continue": {
"grncontinue": "0.720220803439|0.720221273467|12887566|0",
"continue": "grncontinue||"
},
"query": {
"pages": {
"4897672": {
"pageid": 4897672,
"ns": 0,
"title": "New Hope, Sunnyvale, Texas"
}
}
}
}
query
和pages
始终存在,并且在页面中键总是随机生成的,因此它是Map
Map的String
键和JSONObject
作为值,然后需要从映射值中获取title
值
String API = "https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//open connection with wikipedia.
HttpURLConnection httpcon = (HttpURLConnection) new URL(API).openConnection();
//read all the input from wikipedia.
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String responseSB = in.lines().collect(Collectors.joining());
in.close();
JSONObject incomingJSON = new JSONObject(responseSB);
Map<String,JSONObject> map = (Map<String, JSONObject>) incomingJSON.getJSONObject("query").getJSONObject("pages");
map.forEach((k,v)->System.out.println(" The key is : "+k+" the title is : "+v.getString("title")));
因此,source
可能不会出现在每个响应中,请使用try-catch进行处理
String API = "https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//open connection with wikipedia.
HttpURLConnection httpcon = (HttpURLConnection) new URL(API).openConnection();
//read all the input from wikipedia.
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String responseSB = in.lines().collect(Collectors.joining());
in.close();
JSONObject incomingJSON = new JSONObject(responseSB);
Map<String,JSONObject> map = (Map<String, JSONObject>) incomingJSON.getJSONObject("query").getJSONObject("pages");
map.forEach((k,v)->{
System.out.println(" The key is : "+k+" the title is : "+v.getString("title"));
//use try catch to get source because you will not get the same response every time
String source = v.getJSONObject("thumbnail").getString("source");
});
}
stringapi=”https://en.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=pageimages&format=json&pithumbsize=500&utf8";
//打开与维基百科的连接。
HttpURLConnection httpcon=(HttpURLConnection)新URL(API).openConnection();
//阅读维基百科的所有输入。
BufferedReader in=新的BufferedReader(新的InputStreamReader(httpcon.getInputStream());
字符串responseb=in.lines().collect(collector.joining());
in.close();
JSONObject incomingJSON=新的JSONObject(responseB);
Map Map=(Map)incomingJSON.getJSONObject(“查询”).getJSONObject(“页面”);
图.forEach((k,v)->{
System.out.println(“键为:+k+”,标题为:+v.getString(“标题”);
//使用try-catch获取源代码,因为您不会每次都得到相同的响应
字符串源=v.getJSONObject(“缩略图”).getString(“源”);
});
}
试试这个。。。
试试这个。。。
您应该知道页面id将更改,缩略图是可选的
// new code
JSONObject incomingJSON = new JSONObject(responseSB);
JSONObject pages = incomingJSON.getJSONObject("query").getJSONObject("pages");
Iterator<String> it = pages.keys();
while(it.hasNext()) {
JSONObject page = pages.getJSONObject(it.next());
String mTitle= page.getString("title");
if(page.keySet().contains("thumbnail")) {
String mUrl= page.getJSONObject("thumbnail").getString("source");
}
}
//新代码
JSONObject incomingJSON=新的JSONObject(responseB);
JSONObject pages=incomingJSON.getJSONObject(“查询”).getJSONObject(“页面”);
迭代器it=pages.keys();
while(it.hasNext()){
JSONObject page=pages.getJSONObject(it.next());
String mTitle=page.getString(“title”);
如果(page.keySet()包含(“缩略图”)){
String mUrl=page.getJSONObject(“缩略图”).getString(“源”);
}
}
您应该知道页面id将发生变化,缩略图是可选的
// new code
JSONObject incomingJSON = new JSONObject(responseSB);
JSONObject pages = incomingJSON.getJSONObject("query").getJSONObject("pages");
Iterator<String> it = pages.keys();
while(it.hasNext()) {
JSONObject page = pages.getJSONObject(it.next());
String mTitle= page.getString("title");
if(page.keySet().contains("thumbnail")) {
String mUrl= page.getJSONObject("thumbnail").getString("source");
}
}
//新代码
JSONObject incomingJSON=新的JSONObject(responseB);
JSONObject pages=incomingJSON.getJSONObject(“查询”).getJSONObject(“页面”);
迭代器it=pages.keys();
while(it.hasNext()){
JSONObject page=pages.getJSONObject(it.next());
String mTitle=page.getString(“title”);
如果(page.keySet()包含(“缩略图”)){
String mUrl=page.getJSONObject(“缩略图”).getString(“源”);
}
}
因此,由于ID不断变化,我决定采用另一种方法。
我使用了以下代码:
Pattern p = Pattern.compile("\"source\":\"(.*?)\",\"width");
Matcher m = p.matcher(responseSB);
if (m.find()) {
url = m.group(1);
}
p = Pattern.compile("\"title\":(.*?)\",\"thumbnail");
m = p.matcher(responseSB);
if (m.find()) {
description = m.group(1);
}
因此,由于ID不断变化,我决定采用另一种方法。 我使用了以下代码:
Pattern p = Pattern.compile("\"source\":\"(.*?)\",\"width");
Matcher m = p.matcher(responseSB);
if (m.find()) {
url = m.group(1);
}
p = Pattern.compile("\"title\":(.*?)\",\"thumbnail");
m = p.matcher(responseSB);
if (m.find()) {
description = m.group(1);
}
但它并不总是38690716 OK,尝试使用索引而不是对象名。但它并不总是38690716 OK,尝试使用索引而不是对象名。它不总是38690716它不总是38690716嗨,我得到了错误“语言级别7不支持lambda表达式”,因为我理解它给出这个错误是因为map.forEach行。我不确定什么是lambda表达式。还有其他方法吗?您使用的是jdk 7,lambda表达式来自jdk-8,所以只需使用for循环来迭代mapNo,我在这里发布了另一个解决方案。您好,我收到了错误“lambda表达式在语言级别7不受支持”,因为我知道它会因为map.forEach行而给出此错误。我不确定什么是lambda表达式。还有其他方法吗?您使用的是jdk 7,lambda表达式来自jdk-8,所以只需使用for循环来迭代mapNo,我已经提出了另一个解决方案,我在这里发布了。