我目前使用GSON使用inputstream/reader解析相当大的JSON文件。 解析需要大约35秒,在我的Android设备上,我从某些基准测试中了解到杰克逊的表现更好。但我无法找到如何使用杰克逊解析我的JSON文件。谁能帮我?从Gson更改为Jackson解析JSON
我的JSON是这样的:
[
{
"venue": { … }
},
{
"venue": {
"venue_seasons": [ … ],
"address": "the address",
"city": "the city",
"name": "the name",
"created_at": "2011-05-31T07:55:33Z",
"latitude": 00.000000,
"country": "the country",
"internal_link_en": null,
"internal_link_nl": null,
"updated_at": "2011-09-15T14:46:09Z",
"zipcode": "the zipcode",
"foursquare_link": "foursquare url",
"url": null,
"id": 3,
"tip": "some tip",
"uid": "4ab5e205f964a520317620e3",
"phone": "phonenr",
"recommended": null,
"website": "someurl",
"venue_photos": [ … ], //array containing objects with urls of images
"description": null,
"longitude": 00.000000,
"thumbnail_location": null,
"subcategories": [ … ],
"opening_en": null,
"opening_nl": null,
"hidden": false,
"twitter": "thetwitteraccount",
"themes": [ … ]
}
}, //more venues
]
我GSON代码看起来是这样,它的工作原理:
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("filename.json");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
Reader reader = new InputStreamReader(inputStream);
Gson gson = new Gson();
List<JsonResponse> venueList = gson.fromJson(reader, new TypeToken<List<JsonResponse>>() {}.getType());
JsonResponse naam = venueList.get(12);
String denaam = naam.venue.getName;
Log.i("nr12",denaam);
Log.i("timetracker","stop");
// just some logging to test if the parser works
for (JsonResponse venue : venueList) {
String tijdel = String.valueOf(venue.venue.id);
Log.i(venuetag,"name of venue"+ tijdel+ " is: " + venue.venue.getName);
}
...
class JsonResponse
{
Venues venue;
}
class Venues
{
public List<VenueSeasons> venue_seasons;
public List<VenuePhotos> venue_photos;
public List<SubCategories> subcategories;
public List<Themes> themes;
@SerializedName("address")
public String getAdress;
@SerializedName("city")
public String getCity;
@SerializedName("country")
public String getCountry;
@SerializedName("name")
public String getName;
@SerializedName("created_at")
public Date getCreatedAt;
@SerializedName("updated_at")
public Date getUpdatedAt;
@SerializedName("internal_link_nl")
public String getInternalLinkNl;
@SerializedName("internal_link_en")
public String getInternalLinkEN;
@SerializedName("latitude")
public Double getLatitude;
@SerializedName("longitude")
public Double getLongitude;
@SerializedName("foursquare_link")
public String getFoursquareLink;
@SerializedName("url")
public String getURL;
@SerializedName("phone")
public String getPhone;
@SerializedName("zipcode")
public String getZipCode;
public String tip;
public String tip_en;
public String uid;
public int id;
@SerializedName("website")
public String getWebsite;
@SerializedName("recommended")
public Boolean getRecommended;
@SerializedName("description")
public String getDescription;
@SerializedName("hidden")
public Boolean getHidden;
@SerializedName("opening_en")
public String getOpeningEN;
@SerializedName("opening_nl")
public String getOpeningNL;
@SerializedName("twitter")
public String getTwitter;
@SerializedName("thumbnail_location")
public String getThumbnailLocation;
}
public class VenuePhotos
{
@SerializedName("large")
public String getLargePhotoURL;
@SerializedName("medium")
public String getMediumPhotoURL;
@SerializedName("small")
public String getSmallPhotoURL;
@SerializedName("original")
public String getOriginalPhotoURL;
public String uid;
public int id;
public int venue_id;
public boolean selected;
@SerializedName("created_at")
public Date getCreatedAt;
@SerializedName("updated_at")
public Date getUpdatedAt;
}
现在这个工作。我做了一些与数据的东西,解析后它的作品都很棒,但我认为它吸了我的应用程序的启动需要很长时间。
我的杰克逊码(失败的)是:
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("originalDelftJson.json");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
Reader reader = new InputStreamReader(inputStream);
ObjectMapper objectMapper = new ObjectMapper();
JsonFactory jsonFactory = new JsonFactory();
JJsonResponse response = null;
try {
JsonParser jp = jsonFactory.createJsonParser(reader);
response = objectMapper.readValue(jp, JJsonResponse.class);
String test = String.valueOf(response.venue.size());
带班:
public class JJsonResponse
{
public List<JVenue> venue;
}
class Venues
{
public List<VenueSeasons> venue_seasons;
public List<VenuePhotos> venue_photos;
public List<SubCategories> subcategories;
public List<Themes> themes;
@SerializedName("address")
public String getAdress;
@SerializedName("city")
public String getCity;
@SerializedName("country")
public String getCountry;
@SerializedName("name")
public String getName;
@SerializedName("created_at")
public Date getCreatedAt;
@SerializedName("updated_at")
public Date getUpdatedAt;
@SerializedName("internal_link_nl")
public String getInternalLinkNl;
@SerializedName("internal_link_en")
public String getInternalLinkEN;
@SerializedName("latitude")
public Double getLatitude;
@SerializedName("longitude")
public Double getLongitude;
@SerializedName("foursquare_link")
public String getFoursquareLink;
@SerializedName("url")
public String getURL;
@SerializedName("phone")
public String getPhone;
@SerializedName("zipcode")
public String getZipCode;
public String tip;
public String tip_en;
public String uid;
public int id;
@SerializedName("website")
public String getWebsite;
@SerializedName("recommended")
public Boolean getRecommended;
@SerializedName("description")
public String getDescription;
@SerializedName("hidden")
public Boolean getHidden;
@SerializedName("opening_en")
public String getOpeningEN;
@SerializedName("opening_nl")
public String getOpeningNL;
@SerializedName("twitter")
public String getTwitter;
@SerializedName("thumbnail_location")
public String getThumbnailLocation;
}
public class JVenue
{
public String name;
public int id;
public String city;
public String address;
public String country;
public String internal_link_nl;
public String internal_link_en;
public String zipcode;
public String foursquare_link;
public String tip_en;
public String url;
public Date created_at;
public Date updated_at;
public float latitude;
public float longitude;
public String tip;
public String uid;
public String phone;
public String recommended;
public String website;
public String description;
public String thumbnail_location;
public boolean hidden;
public String twitter;
public String opening_en;
public String opening_nl;
}
我觉得我很接近,但即时通讯做错了什么,因为我得到的错误:org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.jacksonrecipes.testapp.model.JJsonResponse out of START_ARRAY token
我真的不明白杰克逊的工作方式以及如何实施它。有谁知道如何在我的Android代码中更改我的杰克逊实现,以便它可以工作,并且我可以访问数据?
编辑:有我的解决方案
随着MH的答案的帮助。我可以找到它。我现在使用:
List<JJsonResponse> venueCounter = objectMapper.readValue(inputStream, new TypeReference<List<JJsonResponse>>() { });
哪个版本GSON的使用是你?我是一名Gson开发人员,我很乐意让您的代码更快运行。如果您可以给我一份完整文档的副本,我们可以将其作为我们的标准基准之一。 – 2012-07-09 03:59:10
快速提问:是否加快了处理速度?由于较大的罐子尺寸,启动时间可能会较长。 – StaxMan 2012-07-09 17:48:30
@StaxMan是的,但没有我希望的那么多。杰克逊在这种情况下快了大约20%,仍然试图更好地优化它。使用更快的android设备,现在可以接受启动时间。 – Jasper 2012-07-19 09:15:59