在Java中动态创建JSON对象

时间:2014-09-17 09:57:33

标签: java json

我试图在我的servlet中构建一个JSON对象。 该对象应如下所示:

{
    "firms": [
    {
        "name": "firm1",
        "projects": [
        {
            "name": "firm1project1"
        },
        {
            "name": "firm1project2"
        },
        {
            "name": "firm1project3"
        }
        ]
    },
    {
        "name": "firm2",
        "projects": [
        {
            "name": "firm2project1"
        },
        {
            "name": "firm2project2"
        },
        {
            "name": "firm2project3"
        }
        ]
    },
    {
        "name": "firm3",
        "projects": [
        {
            "name": "firm3project1"
        },
        {
            "name": "firm3project2"
        },
        {
            "name": "firm3project3"
        }
        ]
    },
    {
        "name": "firm4",
        "projects": [
        {
            "name": "firm4project1"
        },
        {
            "name": "firm4project2"
        },
        {
            "name": "firm4project3"
        }
        ]
    }
    ]
}

我在创建项目名称对象数组时遇到问题:

[
     {
         "name": "firm2project1"
     },
     {
         "name": "firm2project2"
     },
     {
         "name": "firm2project3"
     }
]

现在我的代码如下所示(oJsonInner是一个JSONObject对象,一个项目 - JSONObject类型的ArrayList)。我从数据库查询得到的结果中构建了oJsonInner对象:

while(result.next()){
oJsonInner.put("name",result.getString("project_name"));
aProjects.add(oJsonInner);
}

有没有办法在aProjects.add(oJsonInner);中获取oJsonInner对象的值,所以在下一个循环中我可以使用不同的" project_name"创建一个新的oJsonInner对象。在第一次循环期间没有更新进入aProjects数组的对象的值?

4 个答案:

答案 0 :(得分:4)

while(result.next()){
   oJsonInner = new JsonObject();
   oJsonInner.put("name",result.getString("project_name"));
   aProjects.add(oJsonInner);
}

答案 1 :(得分:1)

试试这个方法:

ArrayList<JSONObject> aProjects = new <JSONObject>ArrayList();

while(result.next()){
    JSONObject oJsonInner = new JSONObject();
    oJsonInner.put("name","project1");
    aProjects.add(oJsonInner);
}

结果:

[{"name":"project1"}, {"name":"project2"}]

答案 2 :(得分:1)

您可以使用JSONArray对象并添加它JSON对象。

while(result.next()){
  JSONObject oJsonInner = new JSONObject();
  JSONArray arr = new JSONArray();
  json.put("name",result.getString("project_name"));
  arr.put(json);
}

答案 3 :(得分:0)

使用Gson lib帮助您序列化json格式,该格式采用java bean并将其转换为json格式。

<强>模型

public class Firm {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

private List<Project> projetcts;


public List<Project> getProjetcts() {
    return projetcts;
}

public void setProjetcts(List<Project> projetcts) {
    this.projetcts = projetcts;
}


public static class Project{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
}

Gson代码

 public static void main(String[] args) {
    Firm [] firms = new Firm[2]; 

    Project p1 = new Project();
    p1.setName("project 1");
    Project p2 = new Project();
    p2.setName("project 2");
    Project p3 = new Project();
    p3.setName("project 3");
    List<Project> projects = new ArrayList<Firm.Project>();
    projects.add(p1);
    projects.add(p2);
    projects.add(p3);

    Firm firm1 = new Firm();
    firm1.setName("firm1");
    firm1.setProjetcts(projects);

    Firm firm2 = new Firm();
    firm2.setName("firm2");
    firm2.setProjetcts(projects);

    firms[0] = firm1;
    firms[1] = firm2;

    String jsonText = new Gson().toJson(firms);
    System.out.println(jsonText);

}

结果示例

  

[{&#34; name&#34;:&#34; firm1&#34;,&#34; projetcts&#34;:[{&#34; name&#34;:&#34; project 1&# 34;},{&#34;名称&#34;:&#34;项目   2&#34;},{&#34;名称&#34;:&#34;项目   3&#34;}]},{&#34;名称&#34;:&#34; firm2&#34;&#34; projetcts&#34;:[{&#34;名称&#34;:&#34 ;项目   1&#34;},{&#34;名称&#34;:&#34;项目2&#34;},{&#34;名称&#34;:&#34;项目3&#34;}]}]