如何解析java中的json以便我将获得两个对象并且我可以迭代它们?

时间:2015-04-23 06:38:43

标签: java json parsing

在JSON下面,有两个数组。我希望它们能够以这样的方式进行解析,即我可以逐个迭代它们并存储结果。

请在下面找到JSON文件的内容。

{
  "id": 1,
  "firstname": "Katerina",
  "languages": [
    {
      "lang": "en",
      "knowledge": "proficient"
    },
    {
      "lang": "fr",
      "knowledge": "advanced"
    }
  ],
  "job": {
    "site": "www.javacodegeeks.com",
    "name": "Java Code Geeks"
  }
}

{
  "id": 2,
  "firstname": "Kati",
  "languages": [
    {
      "lang": "fr",
      "knowledge": "average"
    },
    {
      "lang": "hn",
      "knowledge": "advanced"
    }
  ],
  "job": {
    "site": "www.example.com",
    "name": "Php Code Geeks"
  }
}

2 个答案:

答案 0 :(得分:0)

您的JSON无效,看起来第一个块被复制以创建下一个块。 无论如何你使用Java然后你可以使用JSONProvider(jaxrs库的一部分)并使用它将java对象转换为json或json转换为java对象。你也可以使用谷歌的Gson libabry,在你的库中添加jar或者如果你使用maven那么使用maven依赖

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>

这是Gson http://www.javacreed.com/simple-gson-example/

的一个例子

答案 1 :(得分:0)

我创建了这个示例,因此您可以生成:

使用Gson lib的有效json文件。

读取此文件并将其转换为Object Array java

我希望能帮到你

工作班:

public class Job {

    private String site;
    private String name;

    public Job() {

    }


    public String getSite() {
        return site;
    }
    public void setSite(String site) {
        this.site = site;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Job [site=");
        builder.append(site);
        builder.append(", name=");
        builder.append(name);
        builder.append("]");
        return builder.toString();
    }



}

班级语言:

public class Language {

    private String lang;
    private String knowledge;



    public Language() {

    }
    public String getLang() {
        return lang;
    }
    public void setLang(String lang) {
        this.lang = lang;
    }
    public String getKnowledge() {
        return knowledge;
    }
    public void setKnowledge(String knowledge) {
        this.knowledge = knowledge;
    }
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Language [lang=");
        builder.append(lang);
        builder.append(", knowledge=");
        builder.append(knowledge);
        builder.append("]");
        return builder.toString();
    }

}

Personne课程:

    import java.util.List;


public class Personne {

    private int id;
    private String firstname;
    private List<Language> languages;
    private Job job;

    public Personne() {

    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public List<Language> getLanguages() {
        return languages;
    }
    public void setLanguages(List<Language> languages) {
        this.languages = languages;
    }
    public Job getJob() {
        return job;
    }
    public void setJob(Job job) {
        this.job = job;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Personne [id=");
        builder.append(id);
        builder.append(", firstname=");
        builder.append(firstname);
        builder.append(", languages=");
        builder.append(languages);
        builder.append(", job=");
        builder.append(job);
        builder.append("]");
        return builder.toString();
    }



}

主类:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;


public class JsonGenerate {

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

        List<Personne> personnes = new ArrayList<Personne>();

        List<Language> languages = new ArrayList<Language>();

        Job job  = new Job();

        Language lang = new Language();

        Personne per = new Personne();

        lang.setLang("en");
        lang.setKnowledge("proficient");
        languages.add(lang);

        lang = new Language();
        lang.setLang("fr");
        lang.setKnowledge("advanced");
        languages.add(lang);

        job.setName("Java Code Geeks");
        job.setSite("www.javacodegeeks.com");

        per.setFirstname("Katerina");
        per.setId(1);
        per.setJob(job);
        per.setLanguages(languages);

        personnes.add(per);

        languages = new ArrayList<Language>();
        per = new Personne();

        lang = new Language();
        lang.setLang("fr");
        lang.setKnowledge("average");
        languages.add(lang);
        lang = new Language();
        lang.setLang("hn");
        lang.setKnowledge("advanced");
        languages.add(lang);

        job.setName("Php Code Geeks");
        job.setSite("www.example.com");

        per.setFirstname("Kati");
        per.setId(2);
        per.setJob(job);
        per.setLanguages(languages);

        personnes.add(per);

        //System.out.println(personnes.toString());

         Writer writer = new FileWriter("Output.json");

         Gson gson = new GsonBuilder().create();

         gson.toJson(personnes, writer);

         writer.close();

         BufferedReader bufferedReader = new BufferedReader(new FileReader("Output.json"));
         Type collectionType = new TypeToken<List<Personne>>() {}.getType();
        //convert the json string back to object
        List<Personne> obj = gson.fromJson(bufferedReader, collectionType);
        System.out.println(obj);

    }

}