gson jar(jar包名如何修改)

本文目录
- jar包名如何修改
- 为什么在android studio会出现failed to resolve:com.google.code.gson:gson:2.3
- 怎样在androidstudio中导入gson包
- JsonObject转成Map
- android 解析json用那个包里面的方法比较好呢
- 怎么在eclipse中导入gson和dom4
- 安卓项目怎么导入gson
- Gson 在java 如何使用啊
- 我想添加 gson 包到Maven里面 不成功,怎么版
jar包名如何修改
首先我们要说说为什么要修改 jar 文件的包名,jar 包我们会非常高频率的使用,尤其是用一些其他三方库的时候,往往把 java 代码打包成 jar 文件,方便项目依赖使用。笔者在开发过程中遇到过一些问题,是由于 jar 文件里面的包名和项目或者运行时环境的引用文件包名上存在冲突导致的,这时候我们修改 jar 文件的包名是一个比较快速有效的解决办法。举两个例子,是笔者亲身经历的两个情况。
Gson库:这个相信很多人都在使用,但是直接导入 gson 的 jar 包,在 HTC Desire HD 这款手机上会抛出异常(TypeNotFoundException)
Jackson库:这也是个 json 解析的,我们在做苹果推送后台的时候,后台用 java 写的,使用 java-apns 库,依赖于 Jackson,然后运行就会抛出异常,具体原因没有查,初步怀疑包名冲突,修改之后就解决的。
下面介绍如何修改jar文件的包名,需要用到一个小工具,叫做 jarjar.jar
为什么在android studio会出现failed to resolve:com.google.code.gson:gson:2.3
import com.google.gson.*; import com.google.gson.reflect.*; import java.util.*; public class GsonToObject{ public static void main(String args)throws Exception{ new GsonToObject().test(); } public void test()throws Exception{ String str=“{\“face\“:[{\“position\“:{\“mouth_right\“:{\“y\“: 46.099487,\“x\“: 25.857667},\“mouth_left\“:{\“y\“: 45.579487,\“x\“: 21.151333},\“height\“: 16.410256,\“width\“: 10.666667},\“attribute\“: {\“race\“: {\“value\“: \“Asian\“,\“confidence\“: 99.71640000000001},\“smiling\“: {\“value\“: 95.1095}},\“face_id\“: \“122\“}],\“response_code\“: 200}“; Gson gson = new GsonBuilder() .create(); Type type = new TypeToken《Response》(){}.getType(); Response res = (Response)gson.fromJson(str, type); System.out.println(res); } class Point{ private double x, y; public Point(){} public Point(double x, double y){this.x=x; this.y=y;} public double getX(){return this.x;} public void setX(double x){this.x=x;} public double getY(){return this.y;} public void setY(double y){this.y=y;} public String toString(){ return “Point:“+“x=“+x+“, y=“+y ; } } class Position{ private Point mouth_right, mouth_left; private double height, width; public Position(){} public Position(Point mouth_right, Point mouth_left, double width, double height){ this.mouth_right=mouth_right; this.mouth_left=mouth_left; this.width=width; this.height=height; } public Point getMouth_right(){return this.mouth_right;} public void setMouth_right(Point mouth_right){this.mouth_right=mouth_right;} public Point getMouth_left(){return this.mouth_left;} public void setMouth_left(Point mouth_left){this.mouth_left=mouth_left;} public double getWidth(){return this.width;} public void setWidth(double width){this.width=width;} public double getHeight(){return this.height;} public void setHeight(double height){this.height=height;} public String toString(){ return “Position:“+“mouth_right=“+mouth_right+“, mouth_left=“+mouth_left+“, width=“+width+“, height=“+height ; } } class Race{ private String value; private double confidence; public Race(){} public Race(String value, double confidence){this.value=value; this.confidence=confidence;} public String getValue(){return this.value;} public void setValue(String value){this.value=value;} public double getConfidence(){return this.confidence;} public void setConfidence(double confidence){this.confidence=confidence;} public String toString(){ return “Race:“+“value=“+value+“, confidence=“+confidence ; } } class Smiling{ private double value; public Smiling(){} public Smiling(double value){this.value=value;} public double getValue(){return this.value;} public void setValue(double value){this.value=value;} public String toString(){ return “Smiling:“+“value=“+value ; } } class Attribute{ private Race race; private Smiling smiling; public Attribute(){} public Attribute(Race race, Smiling smiling){this.race=race; this.smiling=smiling;} public Race getRace(){return this.race;} public void setRace(Race race){this.race=race;} public Smiling getSmiling(){return this.smiling;} public void setSmiling(Smiling smiling){this.smiling=smiling;} public String toString(){ return “Attribute:“+“Race=“+race+“, smiling=“+smiling ; } } class Face{ private Position position; private Attribute attribute; private String face_id; public Face(){} public Face(Position position, Attribute attribute, String face_id){ this.position=position; this.attribute=attribute; this.face_id=face_id; } public Position getPosition(){return this.position;} public void setPosition(Position position){this.position=position;} public Attribute getAttribute(){return this.attribute;} public void setAttribute(Attribute attribute){this.attribute=attribute;} public String getFace_id(){return this.face_id;} public void setFace_id(String face_id){this.face_id=face_id;} public String toString(){ return “Face:“+“Position=“+position+“, Attribute=“+attribute+“, face_id=“+face_id ; } } class Response{ ArrayList《Face》 face=new ArrayList《Face》(); int response_code; public Response(){} public Response(ArrayList《Face》 face, int response_code){ this.face=face; this.response_code=response_code; } public ArrayList《Face》 getFace(){return this.face;} public void setFace(ArrayList《Face》 face){this.face=face;} public int getResponse_code(){return this.response_code;} public void setResponse_code(int response_code){this.response_code=response_code;} public String toString(){ return “Response:“+“face=“+face+“, response_code=“+response_code ; } } }
怎样在androidstudio中导入gson包
MySQL
SQLite
Android Studio如何在项目中添加gson-2.2.3.jar?
标签: Android Android-Studio
我尝试使用新的Android Studio,但我似乎无法让它正常工作.
我使用Gson类库来序列化/反序列化JSON对象.但是类库不知何故无法包含在项目里.
我创建一个新的项目只用MainActivity.在/libs文件夹里复制gson-2.2.3.jar并添加它为一个类库.这样在android studio里包含了jar,因此它可以从源文件被引用.
当我试着运行项目,但是它无法被编译,所以我在.gradle文件里添加:
1
compile files(’libs/gson-2.2.3.jar’)
到依赖项里.然后我成功编译了项目,但是当我运行应用程序后,得到ClassDefNotFoundException异常.
解决方法 1:
把Gson jar(在我的项目里,是gson-2.2.4.jar)放入libs文件夹
右击它,然后点击 ’Add as library’
确保编译文件(’libs/gson-2.2.4.jar’)在你的build.gradle文件里
注意:
第二步是关键步骤,如果漏了,将出现你无法运行的问题.
JsonObject转成Map
首先,你先确认你的字符串是否是json格式的,如果是json格式,那你可以使用Gson.jar或json-lib-xx-jdk.jar两个包来自动解析解析。
使用Gson更简单些,只需要导入一个包就可以,但是他如果使用Object解析到int型的话或自动转成double型,需要定义一个准确的类来解析,不能直接使用Object。示例:
Gson gson = new Gson();
Map《String, Object》 map = new HashMap《String, Object》();
map = gson.fromJson(str, map.getClass());
GSON.jar包的下载地址:
你重写下SomeClass 的toString方法,输出你想要的结果,然后,转出来的JSON就不再是内存地址了
android 解析json用那个包里面的方法比较好呢
android 解析json还有用Google出品的Gson比较好,具体步骤为:
1、首先,从 code.google.com/p/google-gson/downloads/list下载GsonAPI:
google-gson-1.7.1-release.zip 把gson-1.7.jar copy到libs(项目根目录新建一个libs文件夹)中。 可以使用以下两种方法解析JSON数据,通过获取JsonReader对象解析JSON数据。
代码如下:
String jsonData = “[{\“username\“:\“arthinking\“,\“userId\“:001},{\“username\“:\“Jason\“,\“userId\“:002}]“;
try{
JsonReader reader = new JsonReader(new StringReader(jsonData));
reader.beginArray();
while(reader.hasNext()){
reader.beginObject();
while(reader.hasNext()){
String tagName = reader.nextName();
if(tagName.equals(“username“)){
System.out.println(reader.nextString());
}
else if(tagName.equals(“userId“)){
System.out.println(reader.nextString());
}
}
reader.endObject();
}
reader.endArray();
}
catch(Exception e){
e.printStackTrace();
}
2、使用Gson对象获取User对象数据进行相应的操作:
代码如下:
Type listType = new TypeToken《LinkedList《User》》(){}.getType();
Gson gson = new Gson();
LinkedList《User》 users = gson.fromJson(jsonData, listType);
for (Iterator iterator = users.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getUsername());
System.out.println(user.getUserId());
}
3、如果要处理的JSON字符串只包含一个JSON对象,则可以直接使用fromJson获取一个User对象:
代码如下:
String jsonData = “{\“username\“:\“arthinking\“,\“userId\“:001}“;
Gson gson = new Gson();
User user = gson.fromJson(jsonData, User.class);
System.out.println(user.getUsername());
System.out.println(user.getUserId());
怎么在eclipse中导入gson和dom4
MySQL
SQLite
Android Studio如何在项目中添加gson-2.2.3.jar?
标签: Android Android-Studio
我尝试使用新的Android Studio,但我似乎无法让它正常工作.
我使用Gson类库来序列化/反序列化JSON对象.但是类库不知何故无法包含在项目里.
我创建一个新的项目只用MainActivity.在/libs文件夹里复制gson-2.2.3.jar并添加它为一个类库.这样在android studio里包含了jar,因此它可以从源文件被引用.
当我试着运行项目,但是它无法被编译,所以我在.gradle文件里添加:
1
compile files(’libs/gson-2.2.3.jar’)
到依赖项里.然后我成功编译了项目,但是当我运行应用程序后,得到ClassDefNotFoundException异常.
解决方法 1:
把Gson jar(在我的项目里,是gson-2.2.4.jar)放入libs文件夹
右击它,然后点击 ’Add as library’
确保编译文件(’libs/gson-2.2.4.jar’)在你的build.gradle文件里
注意:
第二步是关键步骤,如果漏了,将出现你无法运行的问题.
安卓项目怎么导入gson
下载gson包。
将包放入libs文件夹下。
选中右键-build path-add to build path。
导入完成。
Gson 在java 如何使用啊
google的gson,心想google的应该不会差,带着一些好奇心,我开始使用了gson。
经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的字段。
这里提供gson-2.2.4.jar的下载(密码:4Sk4)
下面是一个简单的例子:
//一个 Person 实体
public class Person {
private String name;
private int age;
//以下是get,set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
实体很简单,两个字段,当然实体中的字段也可以是List或者Set类型的。
Gson gson = new Gson();
List《Person》 persons = new ArrayList《Person》();
for (int i = 0; i 《 10; i++) {
Person p = new Person();
p.setName(“name“ + i);
p.setAge(i * 5);
persons.add(p);
}
String str = gson.toJson(persons);
上面的代码重点是Gson对象,它提供了toJason()方法将对象转换成Json字符串,上面代码的str对象值为:
[
{“name“:“name0“,“age“:0},
{“name“:“name1“,“age“:5},
{“name“:“name2“,“age“:10},
{“name“:“name3“,“age“:15},
{“name“:“name4“,“age“:20},
{“name“:“name5“,“age“:25},
{“name“:“name6“,“age“:30},
{“name“:“name7“,“age“:35},
{“name“:“name8“,“age“:40},
{“name“:“name9“,“age“:45}
]
很标准的json数据,很简单吧,呵呵。
下面来看看gson的反序列化,Gson提供了fromJson()方法来实现从Json相关对象到java实体的方法。
在日常应用中,我们一般都会碰到两种情况,转成单一实体对象和转换成对象列表或者其他结构。
先来看第一种:
比如json字符串为:[{“name“:“name0“,“age“:0}]
Person person = gson.fromJson(str, Person.class);
提供两个参数,分别是json字符串以及需要转换对象的类型。
我想添加 gson 包到Maven里面 不成功,怎么版
一般情况下,maven会自动从公网库中下载jar包,如果maven不能自动下载的话,那就自己动下载相应的jar包及jar包所依赖的其它jar包。可以通过以下三种方式将jar应用到jar包中:
1、如果有配置类似nexus这样的服务器的话,可以在3rd party中进行添加,具体如何操作,可以度娘。
2、通过mvn命令行的方式,将jar包安装到本地maven库中。
3、可以在项目中建一个文件夹,将jar包copy到文件夹中,然后在pom.xml 的
dependencies这样添加:
《dependency》
《groupId》yourjarName《/groupId》
《artifactId》yourjarName-api《/artifactId》
《scope》system《/scope》
《version》anyversion《/version》
《systemPath》${project.basedir}/lib/yourjar.jar《/systemPath》
《/dependency》

更多文章:
turtles歌曲(哪位大神有turtles(乌龟组合)的<谢谢>的歌词中文翻译 感激不尽)
2026年10月11日 10:00
金山铁路22号线(请问现在轨道交通22号线金山铁路是个什么情况据说9月28日就开通了啊~~~)
2026年10月11日 09:10
repercussions(都是余波,repercussions和aftermath有什么区别啊)
2026年10月11日 09:00
dropdownlist 绑定(DropDownList 绑定所有项 并 显示指定项)
2026年10月11日 08:50
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40



