簡體   English   中英

無法將json字符串轉換為對象

[英]Can not convert json string to object

我在我的javascript中構建一個JSONObject,然后使用以下代碼將其作為字符串發送到我的servlet:

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"}]並且servlet沒有問題地接收它。

然后我在我的servlet中得到這個:

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字符串轉換回servlet中的對象,以便能夠循環遍歷項目,或者獲取所需的項目。 我正在使用的庫是org.json

我累了:

JSONObject jsonObj = new JSONObject(step);

沒有任何成功。 剛收到此錯誤: Unhandled exception type JSONException我不知道發生了什么。 也許我已經太累了。 我確定我錯過了一些非常小的東西,但我無法發現它。

我知道已被問過數百次。 我知道我會得到大量的投票,但我無法找到我的問題的答案。

我嘗試了你在評論中發布的字符串,它運行正常。 這是完整的代碼:

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庫

這里你的json String轉換為JSONObject。

在你的情況下它沒有發生,因為[]括號表示數組 所以首先是Array,然后是{} JSONObject ,以防你的String。

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();
    }
    }
 }

OUTPUT

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"}

暫無
暫無

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

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