簡體   English   中英

使用Java(JSP)編寫JSON字符串

[英]Write json strings using java (JSP)

我想創建一個如下的json字符串。

"data": [{
    "id": "1",
    "data": "one",
    "color": "008ee4"
},{
    "id": "2",
    "data": "two",
    "color": "008ee4"
}]

到目前為止,我已經提出了這一點。

<%
JSONObject outerObject = new JSONObject();
JSONArray outerArray = new JSONArray();
JSONObject innerObject = new JSONObject();
JSONArray innerArray = new JSONArray();

innerObject.put("label", "1");
innerObject.put("value", "one");
innerObject.put("color", "008ee4");

outerObject.put("data", outerArray);

System.out.println(outerObject.toString());
%>

它給出的輸出像

{"data":[]}

我想刪除這些花括號並在方括號內輸入數據。 請幫我。

更新

 {
    type: "line",
    renderAt: "chartContainer1",
    width: "500",
    height: "300",
    dataFormat: "json",
    "dataSource":  {
"chart": {
    "caption": "Total Revenues from 2008-2013",
    "numberprefix": "$",
    "bgcolor": "FFFFFF",
    "showalternatehgridcolor": "0",
    "plotbordercolor": "008ee4",
    "plotborderthickness": "3",
    "showvalues": "0",
    "divlinecolor": "CCCCCC",
    "showcanvasborder": "0",
    "tooltipbgcolor": "00396d",
    "tooltipcolor": "FFFFFF",
    "tooltipbordercolor": "00396d",
    "numdivlines": "2",
    "yaxisvaluespadding": "20",
    "anchorbgcolor": "008ee4",
    "anchorborderthickness": "0",
    "showshadow": "0",
    "anchorradius": "4",
    "chartrightmargin": "25",
    "canvasborderalpha": "0",
    "showborder": "0"
},



"data": [
    {
        "label": "2009",
        "value": "4400000",
        "color": "008ee4"
    },
    {
        "label": "2010",
        "value": "4800000",
        "color": "008ee4"
    },
    {
        "label": "2011",
        "value": "5500000",
        "color": "008ee4"
    },
    {
        "label": "2012",
        "value": "6700000",
        "color": "008ee4",
        "anchorradius": "7",
        "tooltext": "Historical high"
    },
    {
        "label": "2013",
        "value": "4200000",
        "color": "008ee4"
    }
]
}
 }

上面是主要的json文件。 我只想用我的數據替換數據部分。

像這樣做:

    JSONObject outerObject = new JSONObject();
    JSONArray outerArray = new JSONArray();
    JSONObject innerObject = new JSONObject();
    JSONArray innerArray = new JSONArray();


    innerObject = new JSONObject();
    innerObject.put("label", "1");
    innerObject.put("value", "one");
    innerObject.put("color", "008ee4");

    innerArray.add(innerObject);
    outerObject.put("data", innerArray);

    System.out.println(outerObject.toString());

暫無
暫無

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

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