我有以下json字符串:
String config = "{contact:{\"FIRSTNAME\":\"C_FirstName\",\"EMAIL\":\"C_EmailAddress\"}, "
+ "customobject:{\"CUSTOM_FIELD1\":\"Custom_Field__11\","
+ "\"FIRSTNAME\":\"First_Name1\",\"EMAIL\":\"Email_Address1\"}}";
我需要提取每个对象,即contact
和customobject
为String
,因此最终结果应为:
String contact = "{\"FIRSTNAME\":\"C_FirstName\",\"EMAIL\":\"C_EmailAddress\"}";
String customobject = "{\"CUSTOM_FIELD1\":\"Custom_Field__11\","
+ "\"FIRSTNAME\":\"First_Name1\",\"EMAIL\":\"Email_Address1\"}";
这需要使用Gson
,所以我正在尝试以下方法:
Map<String, String> map = gson.fromJson(config, new TypeToken<Map<String,Object>>(){}.getType());
String json = map.get("contact").toString();
但是我收到以下错误:
java.lang.ClassCastException:com.google.gson.internal.StringMap 无法强制转换为java.lang.String
实现这一目标的正确方法是什么?
答案 0 :(得分:1)
如果只是提取JSON属性值,则不需要反序列化和序列化步骤。您所要做的就是从JSON输入数据中生成JSON树模型并寻址目标对象。在Gson中,有JsonElement
个用于此目的。
private static final JsonParser jsonParser = new JsonParser();
public static void main(final String... args) {
final String config = "{contact:{\"FIRSTNAME\":\"C_FirstName\",\"EMAIL\":\"C_EmailAddress\"}, "
+ "customobject:{\"CUSTOM_FIELD1\":\"Custom_Field__11\","
+ "\"FIRSTNAME\":\"First_Name1\",\"EMAIL\":\"Email_Address1\"}}";
final JsonObject rootObject = jsonParser.parse(config).getAsJsonObject();
System.out.println(rootObject.get("contact"));
System.out.println(rootObject.get("customobject"));
}
输出:
{ “FIRSTNAME”: “C_FirstName”, “电子邮件”: “C_EmailAddress”}
{“CUSTOM_FIELD1”:“Custom_Field__11”,“FIRSTNAME”:“First_Name1”,“EMAIL”:“Email_Address1”}
每个JsonElement
子类都有自己的toString()
实现来生成其JSON字符串表示形式。有关详细信息,请参阅Gson JsonElement
和JsonParser
JavaDocs。
答案 1 :(得分:0)
找到我的解决方案:
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
public class JsonTest {
static String config = "{contact:{\"FIRSTNAME\":\"C_FirstName\",\"EMAIL\":\"C_EmailAddress\"}, "
+ "customobject:{\"CUSTOM_FIELD1\":\"Custom_Field__11\","
+ "\"FIRSTNAME\":\"First_Name1\",\"EMAIL\":\"Email_Address1\"}}";
String contact = "{\"FIRSTNAME\":\"C_FirstName\",\"EMAIL\":\"C_EmailAddress\"}";
String customobject = "{\"CUSTOM_FIELD1\":\"Custom_Field__11\","
+ "\"FIRSTNAME\":\"First_Name1\",\"EMAIL\":\"Email_Address1\"}";
public static void main(String[] args) {
Gson gson = new Gson();
Conf c = gson.fromJson(config, Conf.class);
String contactStr = gson.toJson(c.getContact());
System.out.println(contactStr);
}
class Conf{
Map<String, String> contact;
Map<String, String> co;
public Map<String, String> getContact() {
return contact;
}
public void setContact(Map<String, String> contact) {
this.contact = contact;
}
public Map<String, String> getCo() {
return co;
}
public void setCo(Map<String, String> co) {
this.co = co;
}
}
}
答案 2 :(得分:0)
试一下
创建类配置,联系人和自定义对象,如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/grey_300">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/gcover"
android:id="@+id/relativeLayout">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profPhoto"
android:clickable="true"
android:layout_width="120dp"
android:layout_height="120dp"
android:padding="20dp"
android:src="@drawable/default_prof"
android:scaleType="centerCrop"
app:civ_border_color="@color/trans"
app:civ_border_width="2dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<android.support.v7.widget.AppCompatButton
android:id="@+id/prof_invite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="2dp"
android:background="@color/green_300"
android:drawableLeft="@drawable/icon1"
android:padding="12dp"
android:text="invite friends" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/prof_conf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="2dp"
android:background="@color/ball_D"
android:drawableLeft="@drawable/icon2"
android:padding="12dp"
android:text="settings" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/prof_out"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="2dp"
android:background="@color/colorPrimaryDark"
android:drawableLeft="@drawable/icon"
android:padding="12dp"
android:text="log out" />
</LinearLayout>
现在使用GSON解析它
class Config implements Serializable {
Contact contact;
CustomObject customobject;
public Config() {
this(new Contact("",""), new CustomObject("", "", ""));
}
public Config(Contact contact, CustomObject customobject) {
this.contact = contact;
this.customobject = customobject;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public CustomObject getCustomobject() {
return customobject;
}
public void setCustomobject(CustomObject customobject) {
this.customobject = customobject;
}
}
class Contact implements Serializable{
String FIRSTNAME;
String EMAIL;
public Contact() {
this("","");
}
public Contact(String FIRSTNAME, String EMAIL) {
this.FIRSTNAME = FIRSTNAME;
this.EMAIL = EMAIL;
}
public String getEMAIL() {
return EMAIL;
}
public void setEMAIL(String EMAIL) {
this.EMAIL = EMAIL;
}
public String getFIRSTNAME() {
return FIRSTNAME;
}
public void setFIRSTNAME(String FIRSTNAME) {
this.FIRSTNAME = FIRSTNAME;
}
}
class CustomObject implements Serializable {
String CUSTOM_FIELD1;
String FIRSTNAME;
String EMAIL;
public CustomObject() {
this("", "", "");
}
public CustomObject(String CUSTOM_FIELD1, String FIRSTNAME, String EMAIL) {
this.CUSTOM_FIELD1 = CUSTOM_FIELD1;
this.FIRSTNAME = FIRSTNAME;
this.EMAIL = EMAIL;
}
public String getCUSTOM_FIELD1() {
return CUSTOM_FIELD1;
}
public void setCUSTOM_FIELD1(String CUSTOM_FIELD1) {
this.CUSTOM_FIELD1 = CUSTOM_FIELD1;
}
public String getFIRSTNAME() {
return FIRSTNAME;
}
public void setFIRSTNAME(String FIRSTNAME) {
this.FIRSTNAME = FIRSTNAME;
}
public String getEMAIL() {
return EMAIL;
}
public void setEMAIL(String EMAIL) {
this.EMAIL = EMAIL;
}
}
注意:请遵循命名约定的最佳做法。你使用的是错误的。