0

json.org の JSONArray オブジェクト コンストラクターを使用して、Java Bean のリストを JSON 文字列に変換しようとしています。

ここに豆があります:

package jackiesdogs.bean;

import java.util.*;

public class UploadLog {
    private String logDescription;
    private List<String> headings;
    private List<List<String>> log;

    public UploadLog(String logDescription, List<String> headings, List<List<String>> log) {
        this.logDescription = logDescription;
        this.headings = headings;
        this.log = log;
    }

    public String getLogDescription() {
        return logDescription;
    }

    public void setLogDescription(String logDescription) {
        this.logDescription = logDescription;
    }

    public List<String> getHeadings() {
        return headings;
    }

    public void setHeadings(List<String> headings) {
        this.headings = headings;
    }

    public List<List<String>> getLog() {
        return log;
    }

    public void setLog(List<List<String>> log) {
        this.log = log;
    }

}

そして、これをJSONに変換するために使用しているコードは次のとおりです。

JSONArray outputJSON = new JSONArray(output,false);

私は次のことを期待しています:

[{"headings":[{"Vendor Order Id"}],"logDescription":"You are attempting to upload a duplicate order.","log":[{[{"132709B"}]}]}]

しかし、代わりに私は得る:

[{"headings":[{"bytes":[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}],"empty":false}],"logDescription":"You are attempting to upload a duplicate order.","log":[{}]}]

何か案は?

4

1 に答える 1

0

私は GSON しか知りませんが、これは非常に信頼できます。以下は GSON で動作します。

import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PlayWithGson2 {
    public static void main(String[] args) throws IOException {
        PlayWithGson pwg = new PlayWithGson();

        List<String> headings = new ArrayList<String>();
        headings.add("Vendor Order Id");

        List<List<String>> log = new ArrayList<List<String>>();
        UploadLog ul = new UploadLog("headings", headings, log);

        Gson gson = new Gson();
        String toJson = gson.toJson(ul);
        System.out.println(toJson);
    }
}

版画:

{"logDescription":"headings","headings":["Vendor Order Id"],"log":[]}
于 2013-02-12T20:10:11.750 に答える