簡體   English   中英

json到Java中的表結構api

[英]json to table structure api in Java

我已經在互聯網上搜索了一段時間,以獲得將json轉換為表格格式的API。 我沒有嘗試過的任何代碼。 如果您有任何想法,請直接給我。

例如:傑森

{ “名”: “rinu”, “年齡”: “14”, “手機”:[{ “COUNTRYCODE”:91, “數字”: “99862656”},{ “COUNTRYCODE”:91, “數字”:” 675432 “}],” OtherDetails “:[{” 活動“:真}]}

輸出可以是(任何分隔)

rinu|14|91|99862656|true
rinu|14|91|675432|true

我不需要任何現成的東西,如果我得到類似的東西,我可以重寫它。

您可能需要這樣:

JacksonRead.java

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.codehaus.jackson.map.ObjectMapper;

public class JacksonRead {
    public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();
        try {
            Example example = mapper.readValue(new File("d:\\user.json"),
                    Example.class);

            StringBuilder builder = new StringBuilder();
            int i = 0;
            for (Phone phone : example.getPhone()) {
                builder.append(example.getName()).append("|");
                builder.append(example.getAge()).append("|");
                builder.append(phone.getCountryCode()).append("|")
                        .append(phone.getNumber()).append("|")
                        .append(example.getOtherDetails().get(i).getActive())
                        .append("|");
                builder.append("\n");
            }
            File file = new File("d:\\user.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(builder.toString());
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Example.java

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

import org.codehaus.jackson.annotate.JsonProperty;

public class Example {

    @JsonProperty("name")
    private String name;
    @JsonProperty("age")
    private String age;
    @JsonProperty("Phone")
    private List<Phone> Phone = new ArrayList<Phone>();
    @JsonProperty("OtherDetails")
    private List<OtherDetail> OtherDetails = new ArrayList<OtherDetail>();

    /**
     * 
     * @return The name
     */
    @JsonProperty("name")
    public String getName() {
        return name;
    }

    /**
     * 
     * @param name
     *            The name
     */
    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 
     * @return The age
     */
    @JsonProperty("age")
    public String getAge() {
        return age;
    }

    /**
     * 
     * @param age
     *            The age
     */
    @JsonProperty("age")
    public void setAge(String age) {
        this.age = age;
    }

    /**
     * 
     * @return The Phone
     */
    @JsonProperty("Phone")
    public List<Phone> getPhone() {
        return Phone;
    }

    /**
     * 
     * @param Phone
     *            The Phone
     */
    @JsonProperty("Phone")
    public void setPhone(List<Phone> Phone) {
        this.Phone = Phone;
    }

    /**
     * 
     * @return The OtherDetails
     */

    @JsonProperty("OtherDetails")
    public List<OtherDetail> getOtherDetails() {
        return OtherDetails;
    }

    /**
     * 
     * @param OtherDetails
     *            The OtherDetails
     */

    @JsonProperty("OtherDetails")
    public void setOtherDetails(List<OtherDetail> OtherDetails) {
        this.OtherDetails = OtherDetails;
    }

    @Override
    public String toString() {
        return "Example [name=" + name + ", age=" + age + ", Phone=" + Phone
                + ", OtherDetails=" + OtherDetails + "]";
    }

}

Phone.java

import org.codehaus.jackson.annotate.JsonProperty;

public class Phone {

    @JsonProperty("countryCode")
    private Integer countryCode;
    @JsonProperty("number")
    private String number;

    /**
     * 
     * @return The countryCode
     */
    @JsonProperty("countryCode")
    public Integer getCountryCode() {
        return countryCode;
    }

    /**
     * 
     * @param countryCode
     *            The countryCode
     */
    @JsonProperty("countryCode")
    public void setCountryCode(Integer countryCode) {
        this.countryCode = countryCode;
    }

    /**
     * 
     * @return The number
     */
    @JsonProperty("number")
    public String getNumber() {
        return number;
    }

    /**
     * 
     * @param number
     *            The number
     */
    @JsonProperty("number")
    public void setNumber(String number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "Phone [countryCode=" + countryCode + ", number=" + number + "]";
    }
}

OtherDetail.java

import org.codehaus.jackson.annotate.JsonProperty;

public class OtherDetail {

    @JsonProperty("Active")
    private Boolean Active;

    /**
     * 
     * @return The Active
     */
    @JsonProperty("Active")
    public Boolean getActive() {
        return Active;
    }

    /**
     * 
     * @param Active
     *            The Active
     */
    @JsonProperty("Active")
    public void setActive(Boolean Active) {
        this.Active = Active;
    }

    @Override
    public String toString() {
        return "OtherDetail [Active=" + Active + "]";
    }

}

user.json

{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656"},{"countryCode":91,"number":"675432"}],"OtherDetails":[{"Active":true}]}

我嘗試了帶有json的庫json2flat

{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656"},{"countryCode":91,"number":"675432"}],"OtherDetails":[{"Active":true}]}

它給出了CSV像::

rinu|14|91|99862656|
rinu|14|91|675432  |
rinu|  |  |        |true

但是,如果您將json調整為::

{"name":"rinu","age":"14","Phone":[{"countryCode":91,"number":"99862656","Active":true},{"countryCode":91,"number":"675432","Active":true}]}

它完全按照您的要求提供了csv。

rinu|14|91|99862656|true
rinu|14|91|675432|true

試試看。 畢竟,解決方案取決於用戶想要如何解釋json。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM