0

JSONspringを使用してjacksonテクノロジーと同様にいくつかのpojoをマップしてWebサービスを作成しようとしていますが、リストをマップすると次のようJSONな不正な形式が作成されるため、問題があります。JSON

["idLine":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2",
     "name":"line1",
     "code":"pru1"
]

mapper、または同様のものを作成して、次のfilterような応答を取得したい:

{
 status:"OK",
 data:[
    {"idLine":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2",
     "name":"line1",
     "code":"pru1"
    }]
}

私は何かを試しましObjectMapperたが、結果はありませんでした。ありがとう。

UPDATE この行は、不正な例外を解析して呼び出す行です (外部ライブラリに属しています)。

result = (JSONObject) new JSONTokener(str).nextValue();

そして、私が解析しようとしている JSON は次のとおりです。

[{"idLine":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2","name":"linea1","code":"pru1","colour":"#ff3200","means":"Bus","type":"urban","notes":"","state":true,"timetableInterurbans":[]}]

明確にするために、@ResponseBody を使用して Spring で Pojo をシリアル化し、Android によって応答が取得され、例外をスローする JSONObject を使用して解析されます。

サーバー側の方法:

@RequestMapping(value="/{country}/{cityName}/{type}", method=RequestMethod.GET)
public @ResponseBody List<Line> getUrbanLinesFromCity(@PathVariable String country, @PathVariable String cityName, @PathVariable String type){
    //Get the city pojo by country and city name
    City city = cityManager.searchCityByCountryAndName(country, cityName);
    //Get the lines from the city
    List<Line> lines = null;
    if (city != null){
        lines = lineManager.getLinesByCityAndType(city.getIdCity(), type);
    }
    return lines;
}
4

1 に答える 1

0

最後に、 というヘルパー クラスを使用して解決しましたJSONResponse。これには 2 つの属性があります。1 つはリクエストのステータスを意味し、もう 1 つはデータ レスポンスを意味します。

public class JSONResponse{
    private Object data;
    private boolean status;

    public JSONResponse (Object data){
        this.data = data;
        this.status = this.data != null;
    }

    //getters and setters for both attributes to be serialized
}

@RequestBody JSONResponseあとは、このヘルパー オブジェクトを内部のデータでシリアル化し、すべてのコントローラーの各メソッドの戻り値として使用して、複数の API 呼び出しで一貫した JSON 応答を取得するだけです。

于 2013-05-16T18:22:10.640 に答える