嗨,大家好,我还是一般的Android相关编程和StackOverflow的noobot,所以对我来说不要太苛刻,但是我一直没能找到这样的问题。 : -/将JSON字符串转换为Java/Python(Jython)对象?
所以,我明白,你可以JSON字符串转换为字符串,并通过在Android中org.json束处理一般JSON对象,但这里是我目前的状况:
我需要一个JSON字符串从一个特定的URL(我已经能够成功地做到这一点),并将其制作成一个数组。实际上是两个数组。我使用的框架在Python上运行,并返回一个包含列表的字典(Python中的数组)。但是,它显示为JSON对象。这里是我从URL到我的Java代码的例子:
{"keywords": ["middle east", "syria"], "link": [["middle east", "http://www.google.com/#q=middle east"], ["syria", "http://www.google.com/#q=syria"]]}
正如你所看到的,这是一个两个索引的字典。第一个是具有列表的“关键字”,第二个是包含列表列表的“链接”。这两个列表(第一个和第二个多维列表)是我希望能够在Java中操作的列表。我知道你可以使用JSONArray,但问题是数组存储在一个Python字典中,而我的Android应用程序没有正确地创建一个JSONArray。你们有什么想法可以解决这个问题吗?我很迷茫。这里是我得到实际的JSON字符串代码(代码中的网址是不是每个人都可以访问,它被送达贴在我的机器上):
static public void refreshFeed(){
try{
String url = "http://192.17.178.116:8080/getkw?nextline="+line;
line++;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String input = null;
try {
while ((input = reader.readLine()) != null) {
sb.append(input + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String enter = sb.toString();
feedEntry add = new feedEntry(enter);
addNewEntry(add);
in.close();
} catch(MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
也请注意,这是没有的JSONString被制作成JSONArray。它只是将JSON对象转换为添加到“feedEntry”对象的常规字符串。
不能确定Python的是什么,但偶然在这里... – 2011-04-29 04:06:17
是的,可能是。只是想我会包括所有可能的信息,以便让大家充分理解发生了什么(如果有所作为)。 – Vinay 2011-04-29 04:23:34