如何解析内蒙古JSON对象数组 [英] How to parse Inner json objects arrays
问题描述
我与ESPN体育API工作。任何一个可以帮助我如何解析呢?
I am working with the ESPN Sports API. Can any one help me how to parse this?
我贴我的code,我能够来解析名称和ID&ndash的,请大家帮忙如何解析内部对象项
I am pasting my code, I am able to parse only the name and id–please help how to parse inner objects items.
public class BaseballActivity extends ListActivity{
private static String url = "http://api.espn.com/v1/sports/baseball?apikey=h29yphwtf7893hktfbn7cd5g";
private static final String TAG_SPORTS = "sports";
private static final String TAG_ID = "id";
private static final String TAG_TIMESTAMP = "timestamp";
private static final String TAG_NAME = "name";
private static final String TAG_NEWS = "news";
private static final String TAG_HEADLINES = "headlines";
private static final String TAG_LINKS = "links";
private static final String TAG_API = "api";
private static final String TAG_SPORTS1 = "sports";
private static final String TAG_HREF = "href";
JSONArray sports = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// HashMap for ListView
ArrayList<HashMap<String, String>> sportsList = new ArrayList<HashMap<String, String>>();
// creating Json parser instance
JSONParser jParser = new JSONParser();
// getting Json String from url
JSONObject json = jParser.getJSONFromUrl(url);
try{
// Getting Array of Contacts
sports = json.getJSONArray(TAG_SPORTS);
// looping through All Contacts
for(int i = 0; i < sports.length(); i++){
JSONObject c = sports.getJSONObject(i);
//String news = c.getString(TAG_NEWS);
// String headlines = c.getString(TAG_HEADLINES);
String name = c.getString(TAG_NAME);
// String timestamp = c.getString(TAG_TIMESTAMP);
String id = c.getString(TAG_ID);
// JSONObject links = c.getJSONObject(TAG_LINKS);
// JSONObject api = c.getJSONObject(TAG_API);
// JSONObject sports = c.getJSONObject(TAG_SPORTS1);
// String href = c.getString(TAG_HREF);
HashMap<String, String> map = new HashMap<String, String>();
// map.put(TAG_TIMESTAMP, timestamp);
map.put(TAG_NAME, name);
// map.put(TAG_NEWS, news);
// map.put(TAG_HEADLINES, headlines);
map.put(TAG_ID, id);
// map.put(TAG_HREF, href);
sportsList.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, sportsList,
R.layout.list_item,
new String[]{TAG_NAME,TAG_ID} , new int[] {
R.id.id,R.id.name});
setListAdapter(adapter);
}
}
这是我的链接,我使用
here is my link I am using
推荐答案
我会建议你使用 GSON 。对我来说,更容易与...
I would recomend you to use gson. For me it was much more easier to work with...
编辑:(约code样品)
-
下载GSON-2.2.2.jar把它添加到你的依赖
Download the gson-2.2.2.jar add it to your dependencies
创建一个基类(当你使用一个以上的要求)
Create a base class (when you use more than one request)
public abstract class RequestBase {
public String ToJson(){
Gson gson = new Gson();
return gson.toJson(this);
}
public abstract String getUrl();
protected String getBaseUrl(){
return //Your URL;
}
}
请一类将执行您的要求。
Make a class which will execute your request..
请为请求和响应类。然后,你可以只是getter和setter方法输入它们:
Make classes for the requests and responses. Then you can just enter them with getters and setters:
公共类SomeRequest扩展RequestBase {
@SerializedName(参数1)
私人诠释参数1;
public class SomeRequest extends RequestBase { @SerializedName("Parameter1") private int Parameter1;
public void setParameter1(int Parameter1) {
this.Parameter1= Parameter1;
}
public int getParameter1() {
return Parameter1;
}
@Override
public String getUrl() {
return this.getBaseUrl() +"YOUR/OWN/URL" + Parameter1;
}
}
和
public class SomeResponse {
@SerializedName("responseParameter1")
private int responseParameter1;
@SerializedName("responseParameter2")
private String responseParameter2;
public void setResponseParameter1(int responseParameter1) {
this.responseParameter1= responseParameter1;
}
public int getResponseParameter1() {
return responseParameter1;
}
public void setResponseParameter2(String responseParameter2) {
this.responseParameter2= responseParameter2;
}
public String getResponseParameter1() {
return responseParameter2;
}
}
这篇关于如何解析内蒙古JSON对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!