0

我有一个网络服务,它发送一个类型化的数组列表,我通过 HttpResponse 捕获它,如下所示:

// create GET request
HttpGet httpGet = new HttpGet("http://localhost:8084/MinecraftRestServer/webresources/Items");
// execute GET request
HttpResponse response = client.execute(httpGet);
// check response
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // response OK
    // retreive response
    List<Recipe> recipesList = new ArrayList<Recipe>();
    HttpEntity jsonObj = response.getEntity();
            //What's next?

从 web 服务发送的数组如下所示:

recipesList.add(new Item(1, 11, "diamond_ingot", "Diamond ingot",
                "0,0,0,0,0,0,0,0,1", "air,diamond_ore"));
recipesList.add(new Item(2, 11, "iron_ingot", "Iron ingot",
                "0,0,0,0,0,0,0,0,1", "air,iron_ore"));

并以这种格式出现:

[{"recipeCategory":11,"recipeImageID":"diamond_ingot","recipeDescription":"Diamond ingot","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,diamond_ore","recipeID":1},{"recipeCategory":11,"recipeImageID":"iron_ingot","recipeDescription":"Iron ingot","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,iron_ore","recipeID":2},{"recipeCategory":11,"recipeImageID":"gold_ingot","recipeDescription":"Gold ingot","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,gold_ore","recipeID":3},{"recipeCategory":11,"recipeImageID":"diamond_ore","recipeDescription":"Diamond ore","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,wooden_pickaxe","recipeID":4},{"recipeCategory":11,"recipeImageID":"iron_ore","recipeDescription":"Iron ore","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,wooden_pickaxe","recipeID":5},{"recipeCategory":11,"recipeImageID":"gold_ore","recipeDescription":"Gold ore","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,wooden_pickaxe","recipeID":6},{"recipeCategory":2,"recipeImageID":"diamond_boots","recipeDescription":"Boots (Diamond)","recipeLocations":"0,0,0,1,0,1,1,0,1","usedImages":"air,diamond_ingot","recipeID":7},{"recipeCategory":2,"recipeImageID":"gold_boots","recipeDescription":"Boots (Gold)","recipeLocations":"0,0,0,1,0,1,1,0,1","usedImages":"air,gold_ingot","recipeID":8},{"recipeCategory":2,"recipeImageID":"iron_boots","recipeDescription":"Boots (Iron)","recipeLocations":"0,0,0,1,0,1,1,0,1","usedImages":"air,iron_ingot","recipeID":9},{"recipeCategory":2,"recipeImageID":"diamond_leggings","recipeDescription":"Leggings (Diamond)","recipeLocations":"1,1,1,1,0,1,1,0,1","usedImages":"air,diamond_ingot","recipeID":10},{"recipeCategory":2,"recipeImageID":"gold_leggings","recipeDescription":"Leggings (Gold)","recipeLocations":"1,1,1,1,0,1,1,0,1","usedImages":"air,gold_ingot","recipeID":11},{"recipeCategory":2,"recipeImageID":"iron_leggings","recipeDescription":"Leggings (Iron)","recipeLocations":"1,1,1,1,0,1,1,0,1","usedImages":"air,iron_ingot","recipeID":12},{"recipeCategory":2,"recipeImageID":"diamond_chestplate","recipeDescription":"Chestplate (Diamond)","recipeLocations":"1,0,1,1,1,1,1,1,1","usedImages":"air,diamond_ingot","recipeID":13},{"recipeCategory":2,"recipeImageID":"gold_chestplate","recipeDescription":"Chestplate (Gold)","recipeLocations":"1,0,1,1,1,1,1,1,1","usedImages":"air,gold_ingot","recipeID":14},{"recipeCategory":2,"recipeImageID":"iron_chestplate","recipeDescription":"Chestplate (Iron)","recipeLocations":"1,0,1,1,1,1,1,1,1","usedImages":"air,iron_ingot","recipeID":15},{"recipeCategory":2,"recipeImageID":"diamond_helmet","recipeDescription":"Helmet (Diamond)","recipeLocations":"1,1,1,1,0,1,0,0,0","usedImages":"air,diamond_ingot","recipeID":16},{"recipeCategory":2,"recipeImageID":"gold_helmet","recipeDescription":"Helmet (Gold)","recipeLocations":"1,1,1,1,0,1,0,0,0","usedImages":"air,gold_ingot","recipeID":17},{"recipeCategory":2,"recipeImageID":"iron_helmet","recipeDescription":"Helmet 

我的问题是,如何将它转换回 arraylist ( ArrayList<Item>) 客户端应用程序中已经存在一个 Item 类。

我已经阅读了有关 Gson 库的示例,但在 API 17 中编译时似乎不再包含它。

什么是最简单的方法?

4

6 回答 6

1

如果使用 Eclipse,请从此处下载GSONjar并将其包含在您的项目中。

如果使用 Android Studio,则打开您的build.gradle并将以下内容添加到您的dependencies块中。或者您可以再次选择不使用 maven,只需将 jar 放到您的 lib 文件夹中。

compile 'com.google.code.gson:gson:2.2.4'

接下来,用于GSON构造项目列表。确保您的班级与响应Item.java中的成员名称相同JSON

 List<Recipe> recipesList = new ArrayList<Recipe>();
 HttpEntity jsonObj = response.getEntity();
 String data = EntityUtils.toString(entity);
 Log.d("TAG", data);
 Gson gson = new GsonBuilder().create();
 recipesList = gson.fromJson(data, new TypeToken<List<Item>>() {}.getType());

确保适当地处理异常。

于 2013-10-28T18:53:41.543 回答
0

我认为您应该使用 json-simple 库将字符串 Json 解析为 JsonObject 并转换为简单数据类型。例子:

JSONArray arrJson = (JSONArray) parser.parse("String json");

获取 JSONArray 中的每个元素 JSONObject,然后将其解析为简单数据类型:

long recipeCategory = (long) jsonObject.get("recipeCategory");
于 2013-10-28T18:49:36.080 回答
0
Step 1 : Item obj=new Item;

Step 2: Parse the json formar for example here :

[[Example1][1]

Step 3: while parsing put ur values in obj :

obj.recipeCategory=value1;

Step 4: insret ur obj into arraylist:

arrayList.add(obj);
于 2013-10-28T18:44:03.253 回答
0

您可以使用Jackson来解析传入的 JSON。(快速介绍

如果您已经有一个具有适当属性的类,它可以像这样简单:

public class Items {
    private List<Item> items;
    // getter+setter
}

ObjectMapper mapper = new ObjectMapper();
Items = mapper.readValue(src, Items.class);

有关更多信息,请参阅

于 2013-10-28T18:41:01.247 回答
0

您可以Gson像许多用户所说的那样使用,这是一个使用 RESTfull 客户端的示例Gson

public class RestRequest {
    Gson gson = new Gson();

    public <T> T post(String url, Class<T> clazz,
        List<NameValuePair> parameters) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {
        // Add your data
        httppost.setEntity(new UrlEncodedFormEntity(parameters));
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        StringBuilder json = inputStreamToString(response.getEntity()
                .getContent());
        T gsonObject = gson.fromJson(json.toString(), clazz);
        return gsonObject;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
    }

    // Fast Implementation
    private StringBuilder inputStreamToString(InputStream is)
        throws IOException {
    String line = "";
    StringBuilder total = new StringBuilder();

    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    // Read response until the end
    while ((line = rd.readLine()) != null) {
        total.append(line);
    }

    // Return full string
    return total;
    }

}

用法将是这样的: new RestRequest("myserver.com/rest/somewebservice", SomeClass.class, Arrays.asList(new BasicValuePair("postParameter", "someParameterValue")));

SomeClass.class你的情况会在哪里Recipe[].class。还要检查这个问题以正确处理服务器端错误。

于 2013-10-29T04:07:17.500 回答
-1

伙计,谷歌是你的朋友!快速搜索“android json”或“android json parse”会为您提供一些不错的教程,例如this onethis here

于 2013-10-28T18:40:20.507 回答