0

私は JavaScript で JSONObject を作成し、次のコードを使用して文字列としてサーブレットに送信しています。

insertDtls = function() {
                    var jsonObj = [];
                    jsonObj.push({location: this.location()});
                    jsonObj.push({value: this.value()});
                    jsonObj.push({coverage: this.coverage()});
                    jsonObj.push({validPeriod: this.collateralValidPer()});
                    jsonObj.push({description: this.description()});

                    var b = JSON.stringify(jsonObj);
                    console.log(b.toString());

                     $.ajax({
                             url:"/HDSWFHub/AppProxy",
                             type: 'GET',
                             data: $.extend({WrJOB: "insertDtls", mainData: b}, tJS.getCommonPostData()),
                             dataType: "json",
                             success: function(responseText, status, xhr){
                                               updateViewModel(responseText);
                                           },
                             error: function(jqXHR, textStatus, error){
                                               tJS.manageError(jqXHR);
                                           }
                 });
 },

文字列は次のよう [{"location":"Boston"},{"value":"5"},{"coverage":"15"},{"validPeriod":"08/05/2013"},{"description":"test description"}]になり、サーブレットは問題なくそれを受け取ります。

次に、サーブレットでこれを取得しています:

String step = request.getParameter("mainData");

            JSONObject jsonObj = new JSONObject();
            final JSONObject obj = new JSONObject();
            System.out.println(step);
            try {
                obj.put("viewModel", "index");
                obj.put("WrSESSIONTICKET", sessionTicket);
                response.getWriter().print(obj.toString());
            } catch (final Exception e) {
                logException(request, response, e, true);
            }

アイテムをループできるようにするため、または必要なものを取得するために、JSON文字列をサーブレットのオブジェクトに変換しようとしています。私が使っているライブラリはorg.json

私は疲れました:

JSONObject jsonObj = new JSONObject(step);

成功せずに。このエラーが発生しました: Unhandled exception type JSONException 何が起こっているのかわかりません。多分私はすでに疲れすぎています。本当に小さなものが欠けていると確信していますが、それを見つけることができません。

私はそれが何百回も尋ねられたことを知っています。私はたくさんの反対票を受け取ることを知っていますが、私の問題に対する答えを見つけることができませんでした.

4

2 に答える 2

1

コメントに投稿した文字列を試してみましたが、うまくいきました。完全なコードは次のとおりです。

import org.json.JSONArray;
import org.json.JSONException;

public class jsonArray {
    public static void main(String[] args) {
        String text = "[{\"location\":\"Boston\"},{\"value\":\"5\"},{\"coverage\":\"15\"},{\"validPeriod\":\"08/05/2013\"},{\"description\":\"test description\"}]";

        try {
            JSONArray jsonArray = new JSONArray(text);
            System.out.println(jsonArray.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

ps org.json-20120521.jar ライブラリを使用しています

于 2013-08-01T12:13:43.567 に答える
1

ここで、json String が JSONObject に変換されます。

あなたの場合、[]ブラケットは Array を表すため、発生しません。したがって、最初は配列であり、次に文字列の場合は{}JSONObjectです。

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

public class Test {

    static String str = "[{\"location\":\"Boston\"},{\"value\":\"5\"},{\"coverage\":\"15\"},{\"validPeriod\":\"08/05/2013\"},{\"description\":\"test description\"}]";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
        JSONArray jsonArr = new JSONArray(str);
        System.out.println("JSON ARRAY IS : ");
        System.out.println(jsonArr.toString());
            for(int i =0 ; i<jsonArr.length() ;i++ ){
                JSONObject jsonObj = jsonArr.getJSONObject(i);
                System.out.println();
                System.out.println(i+" JSON OBJECT IS : ");
                System.out.println(jsonObj.toString());

            }
    } catch (org.json.JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
 }

出力

JSON ARRAY IS : 
[{"location":"Boston"},{"value":"5"},{"coverage":"15"},{"validPeriod":"08/05/2013"},{"description":"test description"}]

0 JSON OBJECT IS : 
{"location":"Boston"}

1 JSON OBJECT IS : 
{"value":"5"}

2 JSON OBJECT IS : 
{"coverage":"15"}

3 JSON OBJECT IS : 
{"validPeriod":"08/05/2013"}

4 JSON OBJECT IS : 
{"description":"test description"}
于 2013-08-30T13:52:39.343 に答える