我有这个JSON数组:
[{"id":"101","title":"Oferta 1"},{"id":"102","title":"Oferta 2"},{"id":"103","title":"Oferta del Mes"},{"id":"104","title":"Promoci\u00f3n Facebook"}]
我需要解析这个JSON,但是当我解析它时,我只回忆
{"id":"101","title":"Oferta 1"}
这是我的代码:
try {
JSONObject json = JSONfunctions.getJSONfromURL("URLOFJSON");
Log.i("log_tag", json.toString());
String jsonvalues = json.getString("id");
Log.i("log_tag", jsonvalues);
}
catch (Exception ex)
{
Log.e("log_tag", "Error getJSONfromURL "+ex.toString());
}
我该如何解决这个问题?
谢谢大家。
答案 0 :(得分:1)
问题是你正在使用jsonObject,获取一个json数组并从中检索json对象 虚拟代码
JSonArray ja;
int resultCount = ja.length();
for (int i = 0; i < resultCount; i++)
{
JSONObject resultObject = ja.getJSONObject(i);
String id = resultObject.getString("id");
}
答案 1 :(得分:0)
用于从json数据获取数据使用以下代码行。
String my_info = investors.getProfileAbout(user_id).toString();
LinearLayout about =(LinearLayout) findViewById(R.id.prof_about);
about.setVisibility(View.VISIBLE);
try {
JSONArray array = new JSONArray(my_info);
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
//id = row.getInt('id');
String type1 = row.getString("type");
String person_marks = row.getString("follwed");
String total_posts = row.getString("totalpost");
String componsated = row.getString("ipost");
String mods = row.getString("mods");
String doj = row.getString("bday");
String avatar = row.getString("avatar");
TextView type = (TextView) findViewById(R.id.mebership);
type.setText(type1);
TextView marks = (TextView) findViewById(R.id.marks);
marks.setText(person_marks);
TextView total_pos = (TextView) findViewById(R.id.total_posts);
total_pos.setText(total_posts);
TextView componsated_posts = (TextView) findViewById(R.id.componsated_posts);
componsated_posts.setText(componsated);
TextView moderating = (TextView) findViewById(R.id.moderating);
moderating.setText(mods);
TextView user_name = (TextView) findViewById(R.id.profile_username);
user_name.setText(extras.getString("user_name"));
ImageView userProfilePic = (ImageView) findViewById(R.id.user_profile_img);
userProfilePic.setImageBitmap(mySession.downloadImage(avatar));
TextView dob = (TextView) findViewById(R.id.dob);
dob.setText(doj);
}
} catch (Exception e) {
Log.e("Exception", "Exception when parsing response JSON."+e.getMessage());
}
有关详情,请点击此处http://grabcodes.blogspot.com
答案 2 :(得分:0)
我用这段代码解决了。
URL urlws = new URL(
"URLOFFJSON");
URLConnection tc = urlws.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
Log.i("log_tag", jo.toString());
}
}
这样我就可以获得Web服务JSONArray
全部谢谢