0

I am using Xstream to generate JSON for my application.I want to use JSON for ajax support. When i try

     xstream.alias(classAlias, jsonModel.getClass()); //Note classAlias="records"
responseStream.println(xstream.toXML(jsonModel));

where jsonModel is actually a List of entity objects.Entity class name is Person which is in "beans" package so full qualified name would be beans.Person. OK Person class has properties like id,name,age etc.

Generated JSON is

   {"records":[{"beans.Person":[{"id":21,"name":"Name21","username":"Username21","password":"password21","age":41,"sex":true},{"id":22,"name":"Name22","username":"Username22","password":"password22","age":42,"sex":true},{"id":23,"name":"Name23","username":"Username23","password":"password23","age":43,"sex":true},{"id":24,"name":"Name24","username":"Username24","password":"password24","age":44,"sex":true},{"id":25,"name":"Name25","username":"Username25","password":"password25","age":45,"sex":true},{"id":26,"name":"Name26","username":"Username26","password":"password26","age":46,"sex":true},{"id":27,"name":"Name27","username":"Username27","password":"password27","age":47,"sex":true},{"id":28,"name":"Name28","username":"Username28","password":"password28","age":48,"sex":true},{"id":29,"name":"Name29","username":"Username29","password":"password29","age":49,"sex":true},{"id":30,"name":"Name30","username":"Username30","password":"password30","age":50,"sex":true}]}]}

I used $.getJSON of jquery to get this response on button click event. I checked the status which is "success" every time so this is working well.Problem occurs when i try to access first records i.e. person's information like id,username etc.

I write statements in Javascript as

<script>
        $(document).ready(function() {
            $("input").click(function(){
                $.getJSON("http://mylocalhost:8080/Paging/paging/records?page=3",function(data,status){

                    alert(status);
                    alert(data.records[0].beans.Person[0].id);
                });
            });
        });

    </script>

First alert works but second doesn't.

Then javascript stops working as it does whenever encounters an error.What i can guess is that in records array Person object is followed by package name.This could be the reason because browser may look for beans object in records array which is not there because beans.Person is a single, atomic name in this case as shown in above. But not sure?

If this is actually problem then how can i stop/control XStream to name an object's name as just classname(person) instead of package.classname(beans.person) as it is being named by default.

or If everything is OK in produced JSON object then why can't i see the expected result? Why simple second alert statement is not working.


I think you are missing the g (Global) at the end of your regex (demo):

alert("abc:target12 target12 cdtarget23 target".replace(/target[0-9]{2}/g, 'ok'))
4

1 回答 1

0

我得到了我要使用的答案

 alert(data.records[0]["beans.Person"][0].id);

代替

 alert(data.records[0].beans.Person[0].id);

一个有用的链接

http://www.javascripttoolbox.com/bestpractices/#squarebracket

于 2013-04-20T07:36:44.587 回答