黑莓写作JSON

时间:2013-02-09 08:06:46

标签: json blackberry

我正在尝试在J2me Blackberry中创建这个JSON。我试过了,但我没有得到怎么做。 这是我正在研究的代码。

JSONObject jArray= new JSONObject();
JSONObject jObjectData = new JSONObject();

    try { 
          jObjectData.put("name", name);
          jObjectData.put("scaleItems", 2); 
          jArray.accumulate("friends",jObjectData);
        } 
       catch (JSONException e) {
                    // TODO Auto-generated catch block
                       e.printStackTrace();
                      }

以下是我想要实现的Json。

 {
        "newScale": {
            "name": "scaleName",
            "scaleitems": [
                {
                    "name": "option 1"
                },
                {
                    "name": "option 2"
                },
                {
                    "name": "option 3"
                },
                {
                    "name": "option 4"
                },
                {
                    "name": "option 5"
                },
                {
                    "name": "option 6"
                }
            ]
        }
    }

1 个答案:

答案 0 :(得分:0)

我刚将它附加到String Buffer。

String Buffer rjson = new StringBuffer("{ \"groupid\": \"" + groupId
                                + "\",");
                        rjson.append("\"question\": \"" + questionArea.getText()
                                + "\",");
                        rjson.append("\"hasComment\": \""
                                + checkBoxComments.getChecked() + "\",");
                        rjson.append("\"endDate\": \"" + fromTo.getText() + "\",");
                        rjson.append("\"newScale\":{");
                        rjson.append("\"name\": \"customName\",");
                        rjson.append("\"scaleItems\": [");

                        rjson.append("{");
                        rjson.append("\"name\": \"" + Option1.getText() + "\" ");
                        rjson.append("},");

                        rjson.append("{");
                        rjson.append("\"name\": \"" + Option2.getText() + "\" ");
                        rjson.append("},");

                        rjson.append("{");
                        rjson.append("\"name\": \"" + Option3.getText() + "\" ");
                        rjson.append("},");

                        rjson.append("{");
                        rjson.append("\"name\": \"" + Option4.getText() + "\" ");
                        rjson.append("},");

                        rjson.append("{");
                        rjson.append("\"name\": \"" + Option5.getText() + "\" ");
                        rjson.append("},");

                        rjson.append("{");
                        rjson.append("\"name\": \"" + Option6.getText() + "\" ");
                        rjson.append("}");

                        rjson.append("]}}");