如何将json数据拆分为字符串

时间:2012-08-01 07:27:17

标签: ajax json jsp spring-mvc

我怀疑如何将json数据拆分成字符串。我的意图是iam动态创建几个文本框并使用ajax发布它们,以避免完全回发,这里检查我的ajax部分

function profileAndSectin_Submit() {
        //alert('test123');
        document.getElementById("hiddenSection").value = i;
        $('#profile_form').submit(
                function() {
                alert(i);
                    var profileName = $('#profileName').val();
                    var Section1 = $('#Section1').val();
                    var dynamicData = " ";
                    for ( var m = 2; m <= i; m++) {
                        var textbx = $('#Section' + m).val();//"Section"+m;
                        var dt="section"+m+":"+textbx;
                        //var txtbxval = document.getElementsByName(textbx).value;
                        var x =  textbx ;
                        if (m <= m - 1) {
                            x + " ";
                        }
                        dynamicData = dynamicData +dt+",";
                        //dynamicData = dynamicData +":"+dt+",";

                    }
                    alert(dynamicData);
                    var Data = "profileName :"+profileName+","+"Section1 :"+Section1+"," + dynamicData;


                    alert(Data);

                    $.ajax({

                        type : "post",
                        url : "addProfile",
                        //data : {
                        //"profileName" : profileName,
                        //"Section1" : Section1,
                        //},
                        data :{"Count" : i,"Data" :Data},
                        success : function(msg) {
                            alert(i);
                            $('#divContent').load('addfields.jsp');
                        },
                        Error : function(msg) {
                            debugger;
                        }
                    });

                    return false;
                });
    };

并且iam将这些值发送到一个控制器,主要问题在这里出现,如何使用@RequestParam("Data")字符串数据然后iam将总数据作为单个字符串,如< / p>

{ProfileName:profileName,Section1:section1,Section2:section2...}

但我希望每个字符串如profileName,Section2和Section3都像我想要的那样。

Controller.java

@Controller
public class SettingController implements HibernateConfig {
    @RequestMapping(value = "/addProfile", method = RequestMethod.POST)
     //public String home(HttpServletRequest request,@RequestParam("Count") int i,@RequestParam("profileName") String pname,@RequestParam("Section1") String Section1,Locale locale, Model model) throws IOException
     public String home(HttpServletRequest request,@RequestParam("Count") int i,@RequestParam("Data") String data,Locale locale, Model model) throws IOException

     {

        System.out.println(i);
        System.out.println(data);
}

所以任何想法的人?

2 个答案:

答案 0 :(得分:3)

在Java中使用JSON API / marshaller将JSON字符串转换回Java对象。有许多这样的JSON API:查看http://www.json.org/以获取Java JSON API列表。

答案 1 :(得分:1)

在这种特殊情况下,快速选项是调用您的控制器data.split(",")。您将获得具有分隔值的String[]

在您的示例中,生成的数组将为:{"ProfileName:profileName", "Section1:section1", "Section2:section2", ...}

之后,您可以迭代它并根据需要进行处理。