首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法将json字符串转换为对象。

无法将json字符串转换为对象。
EN

Stack Overflow用户
提问于 2013-08-01 11:25:44
回答 2查看 3K关注 0票数 0

我正在我的javascript中构造一个JSONObject,然后使用以下代码将它作为字符串发送到servlet:

代码语言:javascript
运行
复制
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中得到了这样的信息:

代码语言:javascript
运行
复制
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

我累了:

代码语言:javascript
运行
复制
JSONObject jsonObj = new JSONObject(step);

百无所成。刚刚得到了一个错误:Unhandled exception type JSONException,我不知道发生了什么。也许我已经太累了。我肯定我错过了一些很小的东西,但我找不到它。

我知道它已经被问了几百次了。我知道我会得到大量的选票,但我无法找到我的问题的答案。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-01 12:13:43

我试过了你在评论中发布的字符串,效果很好。以下是完整的代码:

代码语言:javascript
运行
复制
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();
        }
    }
}

附注:我正在使用org.json-20120521.jar库

票数 1
EN

Stack Overflow用户

发布于 2013-08-30 13:52:39

在这里,json字符串被转换为JSONObject。

在您的例子中,它不会发生,因为[] 括号表示数组。因此,首先是Array,然后是{} JSONObject,以防出现字符串。

代码语言:javascript
运行
复制
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();
    }
    }
 }

输出

代码语言:javascript
运行
复制
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"}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17992753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档