我是Android新手。我对Android中的JSON解析有所了解。我正在解析我要解析名为“user”的标记的Twitter Feed,其中包含profile_image_url
和screen_name
。
JSON Feed是here。这是我的代码:
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(USER);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
}
答案 0 :(得分:3)
您可以在android中使用JSONObject。
JSONParser jParser = new JSONParser();
// getting JSON string from URL
String jsonString = jParser.getJSONFromUrl(url).tostring();
JSONArray json= new JSONArray(jsonString);
try {
// looping through All Contacts
for(int i = 0; i < json.length(); i++){
JSONObject contacts = json.getJSONObject(i);
JSONObject user = contacts.getJSONObject("user");
String imageUrl = user.getString("profile_image_url");
String screenName = user.getString("Screen_name");
}
}catch(Exception e)
{
}
答案 1 :(得分:1)
您可以使用json.org
库来轻松使用Android中包含的JSON文件。
然而,您可以使用Gson from Google, or Jackson
来执行此操作....
查看此网站非常有用:
http://www.vogella.com/articles/AndroidJSON/article.html
以上网站还解释了twitter Json解析..
<强>例如强>
JSONObject jsonObject = jsonArray.getJSONObject(i);
jsonObject.getString("name");
答案 2 :(得分:1)
使用Gson
。检查user guide。
示例模型:
Class User
{
public String profile_image_url;
public String screen_name;
}
示例分析器:
Gson gson = new Gson();
User user = gson.fromJson("['profile_image_url':'the_url', 'screen_name':'the_name']", User.class);
PS:确保json中的profile_image_url
名称与Class的成员名称完全相同。
答案 3 :(得分:0)
这是我从Youtube获取频道列表的代码,我希望这有帮助
public void getYouTubeData(){
StringBuilder urlString = new StringBuilder();
HttpURLConnection urlConnection = null;
URL url = null;
urlString.append("http://gdata.youtube.com/feeds/api/users/CaliforniaDMV/playlists?v=2&alt=json");
m_ytdata = new ArrayList<YoutubeChannelList.YouTubePlayListData>();
try {
url = new URL(urlString.toString());
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
urlConnection.setRequestMethod("GET");
} catch (ProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
try {
urlConnection.connect();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
InputStream inputStream = null;
try {
inputStream = urlConnection.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = null;
if(inputStream != null){
result = convertStreamToString(inputStream);
} else {
Log.d("NULLLL","NULL");
}
try {
inputStream.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
urlConnection.disconnect();
try {
final JSONObject json = new JSONObject(result);
final JSONObject jsonentry = json.getJSONObject("feed");
int length = jsonentry.length();
//final JSONObject jsonarr = jsonentry.getJSONObject("entry")
final JSONArray jsonarr = jsonentry.getJSONArray("entry");
int len = jsonarr.length();
//Log.d("XXX", "Length is " + length + "arry len: " + len);
for(int loop = 0;loop<len;loop++){
YouTubePlayListData yt = new YouTubePlayListData();
final JSONObject inner = jsonarr.getJSONObject(loop);
yt.setIds(inner.getJSONObject("yt$playlistId").getString("$t"));
//yt.setVideoId(getThumbnailImage(inner.getJSONObject("yt$playlistId").getString("$t")));
yt.setUpdated(inner.getJSONObject("updated").getString("$t").substring(0,10));
yt.setTitles(inner.getJSONObject("title").getString("$t"));
yt.setCount(Integer.parseInt(inner.getJSONObject("yt$countHint").getString("$t")));
//Log.d("LOOP","id: " +inner.getJSONObject("yt$playlistId").getString("$t"));
//Log.d("LOOP","updated: " + inner.getJSONObject("updated").toString());
//Log.d("LOOP","title: " + inner.getJSONObject("title").getString("$t"));
m_ytdata.add(yt);
}
runOnUiThread(returnRes);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 4 :(得分:0)
像GSON oder jackson这样的拉解析器更适合你的任务(杰克逊声称更快,但也更重的罐子和更多的依赖 - 我更喜欢GSON)
您也可以考虑使用简单的数据绑定库:
https://github.com/ko5tik/jsonserializer
(来自maven central的罐子)
通过它解析JSON会变成这样:
(只提供java.io.Reader和类来填充字段)