【问题标题】:How to get json array name if match with condition [closed]如果与条件匹配,如何获取 json 数组名称 [关闭]
【发布时间】:2013-09-20 10:54:29
【问题描述】:

我首先将 JSON 数组“日期”存储在 arratstring 中,如果匹配,则将我的日期与列表匹配,然后在此行之后显示该日期的“标题”名称if (Vacation_Date.contains(mydate)) {

我只想在 textview 中打印与该日期标题名称匹配的值,请问我该怎么做?

static ArrayList<Long> Vacation_ID = new ArrayList<Long>();
static ArrayList<String> Vacation_name = new ArrayList<String>();
static ArrayList<String> Vacation_Date = new ArrayList<String>();
         JSONObject json3 = new JSONObject(str2);
        status = json3.getString("status");
        if (status.equals("1")) {
        JSONArray school = json3.getJSONArray("data");
        for (int k = 0; k < school.length(); k++) {
            JSONObject jb = (JSONObject) school.getJSONObject(k);
            Vacation_ID.add((long) k);  
                    Vacation_Date.add(jb.getString("date"));
            }
        }

来自Vacation_Date 中 json 文件的强数组“日期”。现在我将我的日期与 Vacation_Date 进行比较,并检查我的日期是否存在于 Vacation_Date 中,并在 textview 中显示其标题名称。

if (Vacation_Date.contains(mydate))     

//现在我要做什么来显示比赛日期的标题名称

textview.settext ("title name of match date")'
 }

JSON

{"status":1,
"data":
[
    {"id":"1",
    "title":"abc",
    "date":"2013-09-29"},

    {"id":"2",
    "title":"abc1",
    "date":"2013-09-25"},

    {"id":"3",
    "title":"abc",
    "date":"2013-10-05"},

    {"id":"4",
    "title":"abc1",
    "date":"2013-09-27"}
]
}

【问题讨论】:

  • 你能格式化你的代码吗?
  • 在 textview 中显示有什么问题。解析有问题?
  • 我只想在 textview 中显示与 mydate 匹配的标题
  • @smartguy:Houcine 已经回复并且应该可以了。我花了几分钟才真正理解了这个问题。只是一个建议,继续努力把问题解释清楚一点。

标签: java android json date


【解决方案1】:

假设您想在日期等于 2013-10-05 时显示标题,因此创建一个方法来通过在 jsonArray 中搜索标题来检索标题,条件是日期属性:

public String getTitleForDate(String searchDate) {
    for ( int i=0; i<array.length; i++) {
        JSONObject json = array.getJSONObject(i);
        String date = json.optString("date");
        if(date != null && searchDate.equals(date)) {
            return json.optString("title");
        }
    }
    return null;
}

然后像这样在TextView 中显示标题:

if(Vacation_Date.contains(mydate)) {
String title = getTitleForDate(mydate); // i.e : in the case of mydate = "2013-10-05", it will return "abc" title
if(title != null)
    yourTextView.setText(title);
}

【讨论】:

  • if (Vacation_Date.contains(mydate))
  • 告诉我关于我的代码告诉我如果 (Vacation_Date.contains(mydate)) 在 textview 中显示匹配日期标题值,我会在此行之后做什么?我在检查上方更改了我的代码
  • @smartguy:在您的 if 条件之后放置此代码并在方法中传递日期对象。字符串标题 = getTitleForDate("2013-10-05"); // 它将返回 "abc" 标题 if(title != null) yourTextView.setText(title);
  • 正如@Vinothbabu 所说,该方法将在您的活动中声明,并在 textView 中显示标题的代码,将其添加到您的 Vacation_Date.contains(mydate);查看我的编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多