简体   繁体   English

如何将数据列表转换为json

[英]how to convert a List of Data to json

I want the following json ,where List<form> will have list of form_id , form_name , how can I convert this using jsonobject, I am not getting the proper json output. 我想要以下json,其中List<form>将具有form_idform_name列表,如何使用jsonobject进行转换,但我没有得到正确的json输出。 Please help me with this. 请帮我解决一下这个。 Json: JSON:

{
    "forms": [
        { "form_id": "1", "form_name": "test1" },
        { "form_id": "2", "form_name": "test2" } 
    ]
}

The above is the json structure that i need it for a list.Where id ,name is a list from form object 上面是我需要一个列表的json结构。id,name是表单对象的列表

public static JSONObject getJsonFromMyFormObject(List<Form> form) {
    JSONObject responseDetailsJson = new JSONObject();
    JSONArray jsonArray = null;
    System.out.println(form.size());
    for (int i = 0; i < form.size(); i++) {
        JSONObject formDetailsJson = new JSONObject();
        formDetailsJson.put("form_id", form.get(i).getId());
        formDetailsJson.put("form_name", form.get(i).getName());
        jsonArray = new JSONArray();
        jsonArray.add(formDetailsJson);
    }
    responseDetailsJson.put("form", jsonArray);
    return responseDetailsJson;

} 

Facing issue here not getting output as a list 面临的问题在这里无法获得列表输出

The code in the original question is close to achieving the described desired result. 原始问题中的代码已接近实现所描述的预期结果。 Just move the JSONArray instance creation outside of the loop. 只需将JSONArray实例创建移出循环即可。

import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Foo
{
  public static JSONObject getJsonFromMyFormObject(List<Form> form)
  {
    JSONObject responseDetailsJson = new JSONObject();
    JSONArray jsonArray = new JSONArray();

    for (int i = 0; i < form.size(); i++)
    {
      JSONObject formDetailsJson = new JSONObject();
      formDetailsJson.put("form_id", form.get(i).getId());
      formDetailsJson.put("form_name", form.get(i).getName());

      jsonArray.add(formDetailsJson);
    }
    responseDetailsJson.put("forms", jsonArray);
    return responseDetailsJson;
  }

  public static void main(String[] args)
  {
    List<Form> forms = new ArrayList<Form>();
    forms.add(new Form("1", "test1"));
    forms.add(new Form("2", "test2"));

    JSONObject jsonObject = getJsonFromMyFormObject(forms);
    System.out.println(jsonObject);
  }
}

class Form
{
  String id;
  String name;

  Form(String i, String n)
  {
    id = i;
    name = n;
  }

  String getId()
  {
    return id;
  }

  String getName()
  {
    return name;
  }
}

Properbly http://www.roseindia.net/tutorials/json/jsonobject-java-example.shtml will help. 适当地http://www.roseindia.net/tutorials/json/jsonobject-java-example.shtml会有所帮助。

According the comment from Tushar, here the extract from the aboved linked website: 根据Tushar的评论,以下是上述链接网站的摘录:

Now in this part you will study how to use JSON in Java. 现在,在这一部分中,您将学习如何在Java中使用JSON。 To have functionality of JSON in java you must have JSON-lib. 要在Java中具有JSON功能,您必须具有JSON-lib。 JSON-lib also requires following "JAR" files: JSON-lib还需要以下“ JAR”文件:

  1. commons-lang.jar 公地lang.jar

  2. commons-beanutils.jar 公地beanutils.jar

  3. commons-collections.jar 公地collections.jar

  4. commons-logging.jar 公共-logging.jar

  5. ezmorph.jar ezmorph.jar

  6. json-lib-2.2.2-jdk15.jar JSON-LIB-2.2.2-jdk15.jar

JSON-lib is a java library for that transforms beans, collections, maps, java arrays and XML to JSON and then for retransforming them back to beans, collections, maps and others. JSON-lib是一个Java库,用于将bean,集合,地图,java数组和XML转换为JSON,然后将其重新转换回bean,集合,地图等。 In this example we are going to use JSONObject class for creating an object of JSONObject and then we will print these object value. 在此示例中,我们将使用JSONObject类创建JSONObject对象,然后将打印这些对象值。 For using JSONObject class we have to import following package "net.sf.json". 为了使用JSONObject类,我们必须导入以下软件包“ net.sf.json”。 To add elements in this object we have used put() method. 为了在这个对象中添加元素,我们使用了put()方法。 Here is the full example code of FirstJSONJava.java is as follows: 这是FirstJSONJava.java的完整示例代码如下:

FirstJSONJava.java FirstJSONJava.java

    import net.sf.json.JSONObject;

    public class FirstJSONJava
    {
       public static void main(String args[])
       {
        JSONObject object=new JSONObject();
         object.put("name","Amit Kumar");
        object.put("Max.Marks",new Integer(100));
        object.put("Min.Marks",new Double(40));
        object.put("Scored",new Double(66.67));
        object.put("nickname","Amit");
        System.out.println(object);
      }
   } 

To run this example you have to follow these few steps as follows: 要运行此示例,您必须遵循以下几个步骤:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM