在json值数组中找到三个邻居值

时间:2017-02-15 21:52:55

标签: java json json-simple

我找到了三个有价值的邻居(6,33)。这是最好的方法。我使用json-Simple读取文件。如果原点是(6,33),那么输出中的第一项将是最接近(6,33)的坐标,然后是第二个最接近的坐标,然后是第三个最接近的坐标,依此类推对于所有26个输入坐标:

[
    {"id":"a","value":[31,49]},

    {"id":"b","value":[44,67]},

    {"id":"c","value":[93,6]},

    {"id":"d","value":[20,16]},

    {"id":"e","value":[68,53]},

    {"id":"f","value":[71,8]},

    ....,

    {"id":"y","value":[75,92]},

    {"id":"z","value":[32,33]}

]

public class readData {

    public static void main(String[] args) throws FileNotFoundException,
    IOException, ParseException {

        Scanner xInput =  new Scanner(System.in);   
        System.out.println("Enter your X value");
        int x = xInput.nextInt();
        Scanner yInput = new Scanner(System.in);
        System.out.println("Enter your Y value");
        int y = yInput.nextInt();

        JSONParser parser = new JSONParser();
        JSONArray jsonArray = (JSONArray) parser.parse(new FileReader("E:\\data\\coordinates.json"));

        for (Object o : jsonArray) {
            JSONObject data = (JSONObject) o;

            String id = (String) data.get("id");
            System.out.print("id:" + id+"\t");      

            JSONArray value = (JSONArray) data.get("value");
            for(Object v: value){
                System.out.print(v+ " ");
            }
            System.out.println("\n");
        }

        xInput.close();
        yInput.close();
    }
}

1 个答案:

答案 0 :(得分:0)

这项工作对我来说,请结账: 1.只需要一个Scanner实例 2.我使用维度3(x,y,距离)进行计算

首先,我有一个欧几里德计算方法

private static double distance(Double[] p1, Double[] p2){
    return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
}

然后是一种计算距离并设置为点位置3(距离计算)的方法

private static List<Double[]> minimumDistance(List<Double[]> points, Double[] point){

    for(Double[] p : points){
        double d = distance(p, point);
        p[2] = d; //Save the distance at index-3 of point
    }

    Collections.sort(result, new Comparator<Double[]>() {
        @Override
        public int compare(Double[] o1, Double[] o2) {
            return (int)(o1[2] - o2[2]);
        }
    });

    return points; //this list is ordered desc by distance
}

最后,主要

public static void main(String[] args) throws FileNotFoundException,
IOException, ParseException, org.json.simple.parser.ParseException {

    Scanner in =  new Scanner(System.in);   

    System.out.println("Enter your X value");
    int x = in.nextInt();

    System.out.println("Enter your Y value");
    int y = in.nextInt();

    Double[] pointIn = new Double[]{
        Double.valueOf(x),
        Double.valueOf(y),
        0.0
    };


    JSONParser parser = new JSONParser();
    JSONArray jsonArray = (JSONArray) parser.parse(new FileReader("E:\\data\\coordinates.json"));


    List<Double[]> points = new ArrayList<>();

    for (Object o : jsonArray) {
        JSONObject data = (JSONObject) o;

        String id = (String) data.get("id");
        System.out.print("id:" + id+"\t");      

        JSONArray value = (JSONArray) data.get("value");

        Double[] point = new Double[]{
            Double.valueOf(value.get(0).toString()),
            Double.valueOf(value.get(1).toString()),
            0.0
        };

        points.add(point);

        System.out.print("Point: " + ArrayUtils.toString(point));

        System.out.println("\n");
    }

    //Find the neighbors
    List<Double[]> neighbors = minimumDistance(points, pointIn);

    System.out.println("Ordered neighbors: ");

    for(Double[] point : neighbors){
        System.out.println(ArrayUtils.toString(point));
    }

    in.close();
}

输出:

Enter your X value
93
Enter your Y value
5
id:a    Point: {31.0,49.0,0.0}

id:b    Point: {44.0,67.0,0.0}

id:c    Point: {93.0,6.0,0.0}

id:d    Point: {20.0,16.0,0.0}

id:e    Point: {68.0,53.0,0.0}

id:f    Point: {71.0,8.0,0.0}

id:y    Point: {75.0,92.0,0.0}

id:z    Point: {32.0,33.0,0.0}

Ordered neighbor: 
{93.0,6.0,1.0}
{71.0,8.0,22.20360331117452}
{68.0,53.0,54.120236510939236}
{32.0,33.0,67.11929677819934}
{20.0,16.0,73.824115301167}
{31.0,49.0,76.02631123499285}
{44.0,67.0,79.02531240052139}
{75.0,92.0,88.84255736976509}

请注意:我正在使用Apache的ArrayUtils库(org.apache.commons.lang.ArrayUtils)

我希望这很有用。此致!