Android gson序列化问题 [英] Android gson serialization issue

查看:99
本文介绍了Android gson序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用的是json数据类型和 gson 类来序列化我的对象。



这是我的代码关于序列化:

  HttpPost post = new HttpPost(); 
post.setURI(新的URI(我的网址在这里));
HashMap< String,String> hashMap = new HashMap< String,String>();
hashMap.put(username,username);
hashMap.put(userCredentials,new Gson()。toJson(credentials,UCredentials.class));
//用户名和凭证是参数。
post.setHeader(Content-Type,application / json; charset = utf-8);
post.setEntity(new StringEntity(new Gson()。toJson(hashMap)));

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
if(httpResponse.getStatusLine()。getStatusCode()== 200){
//如果成功。
}

除了 hashMap.put(userCredentials,new Gson()。toJson(credentials,UCredentials.class)); 代码行一切正常。但是有了这一行......



Gson类给了我json:

  {
userCredentials:
{
\emailOrUsername \:\asd@sadas.com\,
\password \:\113 \
},
username:sdggfsdgf
}

并且它不是有效的json模式。我需要json输出:

  {
userCredentials:
{
emailOrUsername :asd@sadas.com,
password:113
},
用户名:sdggfsdgf
}

我应该怎么做才能做到这一点?

在此先感谢。 / p>

解决方案

您的第一个问题在这里:

  hashMap.put(userCredentials,new Gson()。toJson(credentials,UCredentials.class)); 

进入HashMap的是一个JSON可解析的字符串。当你跟随它:

  post.setEntity(new StringEntity(new Gson()。toJson(hashMap))); 

该字符串因为不是对象而被转义。



你真正想要做的是这样的:

  HttpPost post = new HttpPost(); 

post.setURI(new URI(my url goes here));
//这里我们说Hashmap把Object当作它的值。
HashMap< String,String> hashMap = new HashMap< String,Object>();
hashMap.put(username,username);
//这里我们把凭证对象放在HashMap
hashMap.put(userCredentials,credentials)中;
//用户名和凭证是参数。
post.setHeader(Content-Type,application / json; charset = utf-8);
类型mapType = new TypeToken< HashMap< String,Object>>(){} .getType();
post.setEntity(new StringEntity(new Gson()。toJson(hashMap,mapType)));

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
if(httpResponse.getStatusLine()。getStatusCode()== 200){
//如果成功。
}

这应该做你想做的事,但因为你说它不起作用....



我不确定UCredentials类。它似乎是你自己的代码。这是我的猜测:

  public class UCredentials {

public String emailOrUsername;

公共字符串密码;




$ b $ p
$ b

这里是简化代码:

  import java.lang.reflect.Type; 
import java.util.HashMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


public class Test {

public static void main(String [] args){

Gson gson = new Gson() ;
HashMap< String,Object> hashMap = new HashMap< String,Object>();

UCredentials uc = new UCredentials();
uc.emailOrUsername =me@somehost.com;
uc.password =pAsSwOrD;

hashMap.put(username,ME);
hashMap.put(userCredentials,uc);

类型mapType = new TypeToken< HashMap< String,Object>>(){} .getType();
System.out.println(gson.toJson(hashMap,mapType));
}

}

这是输出:


{userCredentials:{emailOrUsername:me@somehost.com,password:pAsSwOrD},username :ME}


如果您可以发布您的错误消息,我会尝试帮助调试。 b $ b

I have HttpPost request on my Android application to my .NET Web Api.

I am using json data type and gson class in order to serialize my object.

Here is my code about serialization:

    HttpPost post = new HttpPost();
    post.setURI(new URI("my url goes here"));
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("username", username);
    hashMap.put("userCredentials", new Gson().toJson(credentials, UCredentials.class));
    // username and credentials are parameters.
    post.setHeader("Content-Type", "application/json; charset=utf-8");
    post.setEntity(new StringEntity(new Gson().toJson(hashMap)));

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse httpResponse = httpClient.execute(post);
    if (httpResponse.getStatusLine().getStatusCode() == 200) {
        // If successful.
    }

Except hashMap.put("userCredentials", new Gson().toJson(credentials, UCredentials.class)); code line everything works fine. But with this line...

Gson class gives me json as:

{
    "userCredentials":
        "{
            \"emailOrUsername\":\"asd@sadas.com\",
            \"password\":\"113\"
         }",
    "username":"sdggfsdgf"
}

and it is not valid json pattern. I need json output like:

{
        "userCredentials":
            {
                "emailOrUsername":"asd@sadas.com",
                "password":"113"
            },
        "username":"sdggfsdgf"
}

What should I do to achieve this?

Thanks in advance.

解决方案

Your first issue is here:

 hashMap.put("userCredentials", new Gson().toJson(credentials, UCredentials.class)); 

What is going into the HashMap is a String that is JSON parsable. When you follow that up with:

post.setEntity(new StringEntity(new Gson().toJson(hashMap)));

That string gets escaped since it is not an object.

What you really want to do is something like this:

HttpPost post = new HttpPost();

post.setURI(new URI("my url goes here"));
// Here we say that the Hashmap takes Object as its value.
HashMap<String, String> hashMap = new HashMap<String, Object>();
hashMap.put("username", username);
// Here we put the credentials object in the HashMap
hashMap.put("userCredentials", credentials);
// username and credentials are parameters.
post.setHeader("Content-Type", "application/json; charset=utf-8");
Type mapType = new TypeToken<HashMap<String, Object>>() {}.getType();
post.setEntity(new StringEntity(new Gson().toJson(hashMap, mapType)));

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
    // If successful.
}

This should do what you want, but since you say it doesn't work....

I'm not sure about the UCredentials class. It appears to be your own code. Here's a guess on my part:

public class UCredentials {

    public String emailOrUsername;

    public String password;

}

Here is simplified code:

import java.lang.reflect.Type;
import java.util.HashMap;
import com.google.gson.Gson;    
import com.google.gson.reflect.TypeToken;


public class Test {

    public static void main(String[] args) {

        Gson gson = new Gson();
        HashMap<String, Object> hashMap = new HashMap<String, Object>();

        UCredentials uc = new UCredentials();
        uc.emailOrUsername = "me@somehost.com";
        uc.password = "pAsSwOrD";

        hashMap.put("username", "ME");
        hashMap.put("userCredentials", uc);

        Type mapType = new TypeToken<HashMap<String, Object>>() {}.getType();
        System.out.println(gson.toJson(hashMap, mapType));
    }

}

And this is the output:

{"userCredentials":{"emailOrUsername":"me@somehost.com","password":"pAsSwOrD"},"username":"ME"}

If you can post your error message, I'll try and help debug.

这篇关于Android gson序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆