gpt4 book ai didi

json - 解析数据时出错 org.json.JSONException : Value Not of type java. lang.String 在从 youtube api v3 解析 json 时无法转换为 JSONObject

转载 作者:行者123 更新时间:2023-12-03 06:27:44 25 4
gpt4 key购买 nike

我正在尝试从 youtube api v3 解析 json,如下所示

try {
JSONParser jParser = new JSONParser();
JSONObject json = jParser
.getJSONFromUrl("https://www.googleapis.com/youtube/v3/channels?key=AIzaSyBz9D7Ohyk1hGPJdpsLAPka6rZjPQRjm70&id=UCMyqIESszP2IvTO5iZRu8ZA&part=snippet&fields=items(etag,snippet/title,snippet/description,snippet/thumbnails/high/url)");
Log.d("okies", "okies the json is "+json);
}catch (Exception e) {
e.printStackTrace();
}

以下是我的 JSONParser 类
public class JSONParser {

static InputStream is;
static JSONObject jObj;
static String json;

// constructor
public JSONParser() {
is = null;
jObj = null;
json = "";
}

public JSONObject getJSONFromUrl(String url) {

// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
System.out.println("my json is "+json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
jObj = null;

Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
//return jObj;
return jObj;

}
}

当我在浏览器上使用我的 google api key 尝试 url 时,它会显示 json,但是当我尝试运行程序时会出现以下错误
Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject

此外,当我尝试其他 json url 时,我得到了 json。这有什么问题?

最佳答案

我改变了我的解析器如下(来自http://androidapphive.blogspot.com/),它工作得很好

public class JSONParser {

public String url=null;

//You can change the playlistId
//You can change the maxResults from 30 to 50 only videos can be fetched at a time
//For more detail go to this link https://developers.google.com/youtube/v3/docs/playlistItems/list#try-it

private static StringBuilder sb;
private JSONObject jObj;

public JSONParser2() {
// TODO Auto-generated constructor stub
}

public JSONObject getJsonFromYoutube(String url){
DefaultHttpClient httpclient = new DefaultHttpClient();
Log.e("url",url);
HttpGet getRequest = new HttpGet(url);
getRequest.setHeader("Accept", "application/json");
// Use GZIP encoding
getRequest.setHeader("Accept-Encoding", "gzip"); //
try {
HttpResponse response = (HttpResponse) httpclient
.execute(getRequest);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null
&& contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
String result = readStream(instream);
Log.i("JSON", result);
instream.close();
jObj = new JSONObject(result);
}
} catch (Exception e) {
e.printStackTrace();
}
return jObj;
}
private static String readStream(InputStream is) {
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return sb.toString();
}
}

关于json - 解析数据时出错 org.json.JSONException : Value Not of type java. lang.String 在从 youtube api v3 解析 json 时无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24326649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com