基于Json文件的Java非重复数组

基于Json文件的Java非重复数组,java,android,arrays,json,Java,Android,Arrays,Json,目前,我还是一名大学生,在这个实验室项目上有点挣扎。实际上,我完成了这个实验,但我想挑战一下自己,在我的项目中做了额外的工作。 我有这样的JSON文件: { "human": [ {"name":"Richard", "age":"16", "job":"student", "height":"160cm" }, {"name":"Cindy", "age":"17", "job":"student", "height":"150cm" }, {"name":"Yuan", "age":"20"

目前,我还是一名大学生,在这个实验室项目上有点挣扎。实际上,我完成了这个实验,但我想挑战一下自己,在我的项目中做了额外的工作。 我有这样的JSON文件:

{
"human":
[
{"name":"Richard",
"age":"16",
"job":"student",
"height":"160cm"
},
{"name":"Cindy",
"age":"17",
"job":"student",
"height":"150cm"
},
{"name":"Yuan",
"age":"20",
"job":"teacher",
"height":"180cm"
},
{"name":"Kathy",
"age":"18",
"job":"student",
"height":"175cm"
},
{"name":"Lee",
"age":"23",
"job":"teacher",
"height":"165cm"
},
Spinner job
Spinner name
Textview txtview
Textview dispInfo
在我的XML代码中,我的布局如下:

{
"human":
[
{"name":"Richard",
"age":"16",
"job":"student",
"height":"160cm"
},
{"name":"Cindy",
"age":"17",
"job":"student",
"height":"150cm"
},
{"name":"Yuan",
"age":"20",
"job":"teacher",
"height":"180cm"
},
{"name":"Kathy",
"age":"18",
"job":"student",
"height":"175cm"
},
{"name":"Lee",
"age":"23",
"job":"teacher",
"height":"165cm"
},
Spinner job
Spinner name
Textview txtview
Textview dispInfo
这就是我如何试图让我的纺纱机只有教师和学生的工作,但它不工作

    JSONObject myJSON_object = new JSONObject(myText);

    //the main JSON object is/includes an array
    //extract that array
    final JSONArray myJSON_array = myJSON_object.getJSONArray("human");

    //temporarily array for Nettle Spinner
    String[] temp = new String[myJSON_array.length()];
    for (int i=0; i < myJSON_array.length(); i++){
        try{
            //get an individual element of the JSON array
            JSONObject aJSON_element = myJSON_array.getJSONObject(i);
            //get the individual properties of the JSON element
            String jsJob = aJSON_element.getString("job");
            temp[i] = jsJob;

        }
        catch (JSONException e)
        {
            Toast.makeText(getBaseContext(), "Dude, you have to know what the JSON looks like to parse it", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    //actually how the array gonna be in
    String[] forJob = null;
    for (int i=0; i < temp.length; i++){
        boolean duplicate = false;
        int b = 0;
        while (b < i){
            if (temp[i] == temp[b]) {
                duplicate = true;
                b++;
            }
        }
        if (duplicate == false) {
            forNettle[i] = temp[i];
        }
    }

    //associate the full name with the listView
    Spinner spJob = (Spinner) findViewById(R.id.spNettle);
    Spinner spName = (Spinner) findViewById(R.id.spCate);


    ArrayAdapter<String> arrJob = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, forJob);
    arrJob.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
    spJob.setAdapter(arrJob);
JSONObject myJSON\u object=新的JSONObject(myText);
//主JSON对象是/包括一个数组
//提取该数组
final JSONArray myJSON_数组=myJSON_object.getJSONArray(“human”);
//荨麻纺纱机临时阵列
String[]temp=新字符串[myJSON_array.length()];
for(int i=0;i
删除重复项的方法:

int lastIdx = 0;
String[] result = new String[temp.lenght];
mainLoop: for(int i =0; i < temp.length; i++) {
   for(int j = 0; j < lastIdx; j++)
      if(result[j].equals(temp[i])) continue mainLoop;
   result[lastIdx++] = temp[i];
}
int lastIdx=0;
字符串[]结果=新字符串[温度长度];
mainLoop:for(int i=0;i

然后,您的唯一列表将是从0到lastIdx的数组结果位置(其余将用null填充;

从列表中删除重复项的最简单方法是将其转换为集合并返回。集合本身没有重复项,因此您几乎不必执行任何操作

Set<String> tempSet = new HashSet()<> (Arrays.asList(temp));

String[] no_duplicates = tempSet.toArray(new String[tempSet.size()]);
Set tempSet=newhashset()(Arrays.asList(temp));
String[]no_duplicates=tempSet.toArray(新字符串[tempSet.size()]);
或者作为一句甜美、简洁的一句话:

String[] no_duplicates = new HashSet<String>(Arrays.asList(temp)).toArray(new String[temp.length]);
String[]no_duplicates=新哈希集(Arrays.asList(temp)).toArray(新字符串[temp.length]);

您的错误是什么?我不太确定,但我确定我的错误在循环中的某个地方。因为我尝试不使用包含{student,student,teacher,student,teacher}的数组。但是当我运行它时,它运行得非常好,但spiner是空的