如何从json String中提取值

时间:2022-05-14 14:48:08

I am trying to extract a value from a json String which is occurred many times in the String.In my code i am getting only the last occurrence of that String.I am posting my json String and java code

我试图从字符串中多次出现的json字符串中提取一个值。在我的代码中我只得到该字符串的最后一次出现。我发布了我的json字符串和java代码

    public class JsonDemo{

public static void main(String args[]) 

// let's read
readJson("book.json");
 }
 public static void readJson(String file) {
JSONParser parser = new JSONParser();

try {
    System.out.println("Reading JSON file from Java program");
    FileReader fileReader = new FileReader(file);
    JSONObject json = (JSONObject) parser.parse(fileReader);


    for(int  i=0;i<json.size();i++){

         String title = (String) json.get("title");
          System.out.println("title: " + title);
    }

   /* String title = (String) json.get("title");
    String author = (String) json.get("author");
    Long price = (Long) json.get("price");


    System.out.println("author: " + author);
    System.out.println("price: " + price);

    JSONArray characters = (JSONArray) json.get("characters");
    Iterator i = characters.iterator();

    System.out.println("characters: ");
    while (i.hasNext()) {
        System.out.println(" " + i.next());
    }*/

} catch (Exception ex) {
    ex.printStackTrace();
  }
   } 

This is my json String

这是我的json String

 [{jobId=501, candidateId=11824, intAvailability=[{"title":"Phone","start":"2015-10-18T00:00:00.000Z","end":"2015-10-18T02:00:00.000Z"},{"title":"Phone","start":"2015-10-19T00:00:00.000Z","end":"2015-10-19T02:00:00.000Z"},{"title":"Phone","start":"2015-10-20T00:00:00.000Z","end":"2015-10-20T02:00:00.000Z"},{"title":"On site","start":"2015-10-25T00:00:00.000Z","end":"2015-10-25T02:00:00.000Z"},{"title":"On site","start":"2015-10-26T00:00:00.000Z","end":"2015-10-26T02:00:00.000Z"},{"title":"On site","start":"2015-10-27T00:00:00.000Z","end":"2015-10-27T02:00:00.000Z"},{"title":"On site","start":"2015-10-28T00:00:00.000Z","end":"2015-10-28T02:00:00.000Z"}], appliedIntOrWorkedWithClient=No, otherOppoWithEmployer=No, inhibitYourAbility=}, {jobId=501, candidateId=12201, intAvailability=[{"title":"Phone","start":"2015-10-31T00:00:00.000Z","end":"2015-10-31T01:30:00.000Z"},{"title":"Phone","start":"2015-11-01T00:00:00.000Z","end":"2015-11-01T01:30:00.000Z"},{"title":"Phone","start":"2015-11-02T00:00:00.000Z","end":"2015-11-02T01:30:00.000Z"},{"title":"On site","start":"2015-11-03T00:00:00.000Z","end":"2015-11-03T01:30:00.000Z"},{"title":"On site","start":"2015-11-04T00:00:00.000Z","end":"2015-11-04T01:30:00.000Z"},{"title":"On site","start":"2015-11-05T00:00:00.000Z","end":"2015-11-05T01:30:00.000Z"},{"title":"On site","start":"2015-11-06T00:00:00.000Z","end":"2015-11-06T01:30:00.000Z"}], appliedIntOrWorkedWithClient=No, otherOppoWithEmployer=No, inhibitYourAbility=}]

I am getting only the "Man Potter and Half Blood Prince" value ,but i want both "Harry Potter and Half Blood Prince " and "Man Potter and Half Blood Prince"

我只获得了“曼波特与混血王子”的价值,但我想要“哈利波特与混血王子”和“曼波特与混血王子”

2 个解决方案

#1


1  

Please refer to the discussion: https://esdiscuss.org/topic/json-duplicate-keys

请参阅讨论:https://esdiscuss.org/topic/json-duplicate-keys

Basically, JSON RFC suggests interpreters to reject such case or just return the last one, which is what you have obtained.

基本上,JSON RFC建议解释器拒绝这样的情况,或者只返回最后一个,这是你获得的。

#2


0  

Why don't you try with gson library.

你为什么不尝试使用gson库。

public class Reader {

    public static void main(String[] args) {

        try {
            JsonReader reader = new JsonReader(new FileReader("path_to _ur_jsonfile.json"));

            reader.beginObject();
            while (reader.hasNext()) {

                String name = reader.nextName();

                if (name.equals("author")) {

                    System.out.println(reader.nextString());

                } else if (name.equals("title")) {

                    System.out.println(reader.nextString());

                } else if (name.equals("price")) {

                    System.out.println(reader.nextDouble());

                } else if (name.equals("characters")) {
                    reader.beginArray();
                    while (reader.hasNext()) {
                        String chart = reader.nextString();
                        System.out.println(chart);
                    }
                    reader.endArray();
                } else {
                    reader.skipValue();
                }

            }
            reader.endObject();

            reader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output :

J. K. Rolling
Harry Potter and Half Blood Prince
20.0
Harry
Ron
Hermoinee
Man Potter and Half Blood Prince

download http://www.java2s.com/Code/JarDownload/gson/gson-2.2.2.jar.zip

#1


1  

Please refer to the discussion: https://esdiscuss.org/topic/json-duplicate-keys

请参阅讨论:https://esdiscuss.org/topic/json-duplicate-keys

Basically, JSON RFC suggests interpreters to reject such case or just return the last one, which is what you have obtained.

基本上,JSON RFC建议解释器拒绝这样的情况,或者只返回最后一个,这是你获得的。

#2


0  

Why don't you try with gson library.

你为什么不尝试使用gson库。

public class Reader {

    public static void main(String[] args) {

        try {
            JsonReader reader = new JsonReader(new FileReader("path_to _ur_jsonfile.json"));

            reader.beginObject();
            while (reader.hasNext()) {

                String name = reader.nextName();

                if (name.equals("author")) {

                    System.out.println(reader.nextString());

                } else if (name.equals("title")) {

                    System.out.println(reader.nextString());

                } else if (name.equals("price")) {

                    System.out.println(reader.nextDouble());

                } else if (name.equals("characters")) {
                    reader.beginArray();
                    while (reader.hasNext()) {
                        String chart = reader.nextString();
                        System.out.println(chart);
                    }
                    reader.endArray();
                } else {
                    reader.skipValue();
                }

            }
            reader.endObject();

            reader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output :

J. K. Rolling
Harry Potter and Half Blood Prince
20.0
Harry
Ron
Hermoinee
Man Potter and Half Blood Prince

download http://www.java2s.com/Code/JarDownload/gson/gson-2.2.2.jar.zip