无法将json字符串转换为对象 [英] Can not convert json string to object

查看:568
本文介绍了无法将json字符串转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm constructiong a JSONObject in my javascript and then sending it as a string to my servlet using this code:

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毫无问题地收到它.

The string looks like: [{"location":"Boston"},{"value":"5"},{"coverage":"15"},{"validPeriod":"08/05/2013"},{"description":"test description"}] and the servlet receives it without a problem.

然后我在servlet中获取它:

Then I'm getting this in my 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

I'm trying to convert the JSON string back to object in the servlet in order to be able to loop trough the items, or to get the needed one. The library I'm using is org.json

我很累:

JSONObject jsonObj = new JSONObject(step);

没有任何成功.刚收到此错误: Unhandled exception type JSONException 我不知道发生了什么也许我已经太累了.我确定我缺少一些很小的东西,但是我找不到它.

Without any success. Just got this error: Unhandled exception type JSONException I don't know what is happening. Maybe I'm too tired already. I'm sure that I'm missing something really small, but I'm unable to spot it.

我知道已经被问过数百遍了.我知道我会得到大量的反对意见,但是我找不到我的问题的答案.

I know that it has been asked hundreds of times. I know that I will get tons of downvotes, but I was unable to find an answer for my issue.

推荐答案

我尝试了您在评论中发布的字符串,它可以正常工作.这是完整的代码:

I tried the string you posted in your comment and it works fine. Here is the full code:

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

p.s.我正在使用org.json-20120521.jar库

p.s. I am using org.json-20120521.jar library

这篇关于无法将json字符串转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆