1

i'm trying to transfer a list of objects from a server to a client using a web service. The data should be transfered in json format. I'm using jersey for the JAX-RS implementation and gson to deserialise the json string on the client side. I kept getting a parse error, even though i was using the recommended practice for list deserilization with gson (see below). I have found out that the json string which i am getting in the response is different than the one which i should receive in order for the gson deserilizer to work.

This is the reponse i'm getting from the web service:

{"activity":[{"activityName":"c","id":"3"},{"activityName":"c","id":"3"},{"activityName":"c","id":"3"}]}

But this is the correct response i should receive in order for the gson to deserialize the string to a List: [{"id":3,"activityName":"c"},{"id":3,"activityName":"c"},{"id":3,"activityName":"c"}]

You can see the added header/wrapper "activity" in the begining of the first string.

Here is the web service code:

@Path("ws")
public class IiwsServices {

@POST
@Path("/listActivities")
@Produces(MediaType.APPLICATION_JSON)
public List<Activity> listActivities() {

    List<Activity> list = new ArrayList<Activity>();

    Activity act = new Activity();
    act.setId(1);
    act.setActivityName("a");
    list.add(act);

    act.setId(2);
    act.setActivityName("b");
    list.add(act);

    act.setId(3);
    act.setActivityName("c");
    list.add(act);
    return list;
}

This is the client code:

public class Main {
public static void main(String[] args) {
    try {

        Client client = Client.create();

        WebResource webResource = client
           .resource("http://localhost:8080/iiws.ws.test/ws/listActivities");

        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                   .post(ClientResponse.class);

        if (response.getStatus() != 200) {
           throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatus());
        }

        String output = response.getEntity(String.class);

        System.out.println(output);

        List<Activity> list = new ArrayList<Activity>();

        Activity act = new Activity();
        act.setId(1);
        act.setActivityName("a");
        list.add(act);

        act.setId(2);
        act.setActivityName("b");
        list.add(act);

        act.setId(3);
        act.setActivityName("c");
        list.add(act);

        Gson gson = new GsonBuilder().create();
        String jsonString = gson.toJson(list);

        System.out.println(jsonString);

        List<Activity> list = gson.fromJson(output, new   TypeToken<List<Activity>>(){}.getType()); // <-- Deserialize exception here

        for (int i = 0; i < list.size(); i++) {
            Activity x = list.get(i);
            System.out.println(x);
        }

      } catch (Exception e) {

        e.printStackTrace();

      }

}

****OK I believe I have found the answer****

OK finally found solution Here i found a similar question after googling for jersey and jackson : Jersey client can not deserializable json services

And this helps as well: JSON POJO support

Button line is that if not explicitely defined in the web.xml, jersey+jaxb is used instead of jersey+jackson


Replace and extract matches with regex

How can I get all matches with for example preg_match_all() and also replace the results with an empty string?

I've tried stuff like this:

<?php
$comments   = array();              

$selectors  = preg_replace_callback('~\*(?>(?:(?>([^*]+))|\*(?!\/))*)\*~',
function ($match) {
    $comments[] = $match[0];
    return '';
}, $input);
?>

But that doesn't work very well since the $comment variable doesn't seem to be accessable from the anonymous function. I guess I can make global variables, but I really don't want to mess up the namespace like that

4

1 回答 1

0

从 web 服务器返回的 json 字符串,很明显响应有一个对象,该对象的名称是 activity,它有一个活动数组。

因此,如果您尝试使用此结构来读取响应。

    公共类活动{
        私人 int id;
        私人字符串活动名称;
        公共 int getId() {
            返回标识;
        }
        公共无效setId(int id){
            这个.id = id;
        }
        公共字符串 getActivityName() {
            返回活动名称;
        }
        公共无效 setActivityName(字符串活动名称){
            this.activityName = 活动名称;
        }
   }

    公共类响应{
        私人名单活动;

        公共列表 getActivity() {
            返回活动;
        }

        公共无效setActivity(列表活动){
            this.activity = 活动;
        }

    }

并使用 gson Library 阅读

    Gson gson = 新 Gson();
    响应响应 = gson.fromJson(json, Response.class);

于 2013-05-17T12:06:40.397 回答