【问题标题】:Find unique data from json using android使用android从json中查找唯一数据
【发布时间】:2016-01-27 11:31:28
【问题描述】:

如何使用 andorid 从 json 响应中获取唯一结果。

您好,我需要从 json webservice 获取数据,我已经读取了 json 中的所有数据。但我需要存储来自 json 的唯一值。

是否可以从 json 中获取唯一值?

【问题讨论】:

  • 显示你到目前为止所做的代码和示例JSON
  • json里面的值是什么???
  • 这是我的示例 json 代码 { lastyear: [ { order: "96 36 08", year: "2015", Text: "No" }, { order: "03 59 03", year : "2014", Text: "No" }, { order: "16 50 58", year: "2014", Text: "No" } ]} - 我需要年份的值 - OUTPUT - { 2015 , 2014 }

标签: android json


【解决方案1】:

首先,json格式错误:

{ "lastyear": [ { "order": "96 36 08", "year": "2015", "Text": "No" }, { "order": "03 59 03", "year": "2014", "Text": "No" }, { "order": "16 50 58", "year": "2014", "Text": "No" } ]}

要解析这个数组,你需要这样实现

import org.json.*;
import java.util.ArrayList;

public class MyObject {

private ArrayList<Lastyear> lastyear;


public MyObject () {

}   

public MyObject (JSONObject json) {


    this.lastyear = new ArrayList<Lastyear>();
    JSONArray arrayLastyear = json.optJSONArray("lastyear");
    if (null != arrayLastyear) {
        int lastyearLength = arrayLastyear.length();
        for (int i = 0; i < lastyearLength; i++) {
            JSONObject item = arrayLastyear.optJSONObject(i);
            if (null != item) {
                this.lastyear.add(new Lastyear(item));
            }
        }
    }
    else {
        JSONObject item = json.optJSONObject("lastyear");
        if (null != item) {
            this.lastyear.add(new Lastyear(item));
        }
    }


}

public ArrayList<Lastyear> getLastyear() {
    return this.lastyear;
}

public void setLastyear(ArrayList<Lastyear> lastyear) {
    this.lastyear = lastyear;
}



}

以及子对象:

import org.json.*;


public class Lastyear {

private String order;
private String year;
private String text;


public Lastyear () {

}   

public Lastyear (JSONObject json) {

    this.order = json.optString("order");
    this.year = json.optString("year");
    this.text = json.optString("Text");

}

public String getOrder() {
    return this.order;
}

public void setOrder(String order) {
    this.order = order;
}

public String getYear() {
    return this.year;
}

public void setYear(String year) {
    this.year = year;
}

public String getText() {
    return this.text;
}

public void setText(String text) {
    this.text = text;
}



}

使用方法:

MyObject object = new MyObject(new JSONObject("YOUR_JSON"));
//Verifiy if object.getLastyear != null
int sizeList = object.getLastyear().size;

for(int i = 0; i < sizeList;i++){
    LastYear lastYear = object.getLastyear().get(i)
    Log.e("App",lastYear.getYear);
}

只有唯一的价值,你可以使用:

private static String[] arrRemove(String[] strArray) {
    Set<String> set = new HashSet<String>();
    set.addAll((List<String>) Arrays.asList(strArray));
    return (String[]) set.toArray(new String[set.size()]);
} 

【讨论】:

    【解决方案2】:

    请试试这个代码

        private void readJson(JSONObject jobj) {
        ArrayList<String> list=new ArrayList<String>();
        JSONArray jsonArray = jobj.getJSONArray("lastyear");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject obj = jsonArray.getJSONObject(i);
            String year=obj.getString("year");
            if (!list.contains(year)) {
                list.add(year);
            }
        }
        System.out.println("FirstActivity.readJson()"+list);
    }
    

    【讨论】:

    • 输出是 - {2015,2014,2014}
    【解决方案3】:

    将 json 解析为 java,然后使用 HashSet yearHashSet 存储您的年份值。

    遍历您的数组,然后将其存储在yearHashSet

    最后,您将只有唯一的年份值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多