Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Gson格式错误的json_Java_Android_Json_Gson - Fatal编程技术网

Java Gson格式错误的json

Java Gson格式错误的json,java,android,json,gson,Java,Android,Json,Gson,我使用Gson将我的响应转换为它的模型。当我手动复制json的内容并将其粘贴到字符串中时,它工作得非常完美 像 当我将响应直接传递给Gson时,会出现以下异常: 原因:com.google.gson.stream.MalformedJSON异常:第1行第501列的未终止字符串路径$.contact.picture.links[0]。href 这是我的json: json: {“id”:22798,“创建于”:“2015-05-19T19:31:56+02:00”, “更新时间”:“2015-0

我使用
Gson
将我的响应转换为它的模型。当我手动复制json的内容并将其粘贴到
字符串中时,它工作得非常完美

当我将响应直接传递给
Gson
时,会出现以下异常:

原因:com.google.gson.stream.MalformedJSON异常:第1行第501列的未终止字符串路径$.contact.picture.links[0]。href

这是我的json:

json:

{“id”:22798,“创建于”:“2015-05-19T19:31:56+02:00”,
“更新时间”:“2015-05-21T00:03:15+02:00”,“标题”:“标题” “慈善”、“说明”:“可扣税”:正确,
“禁止捐赠”:false,“关闭时间”:null,“捐赠数量”: 0,“捐赠金额”:0,“请求金额”: 50000,“进度百分比”:空,“联系人”:{ “名称”:“R.Kocyigit”, “图片”:{ “链接”:[ { “rel”:“fill_100x100”, “href”:” }, { “rel”:“原件”, “href”:” } ] },

我的代码:

Gson gson = new Gson();
Model model = gson.fromJson(response, Model.class);

我解决了这个问题。这是在通过网络将流转换为字符串的过程中

当我使用BufferedReader转换为字符串时,Gson工作得很好

BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(stream));
    StringBuffer buffer = new StringBuffer();
    int read;
    char[] chars = new char[1024];
    while ((read = reader.read(chars)) != -1)
        buffer.append(chars, 0, read);

    return buffer.toString();
} finally {
    if (reader != null)
        reader.close();
}

该文件是否包含您输入到Gson的确切字符串?(您打印出来检查了吗?)没有,但如果有特殊字符,我能做什么?我无法在服务器中编辑json。请给我们一个完整、最小且可复制的示例。简短的内容。@Luser_k这是一个“不,文件没有包含我输入到Gson的确切字符串”或“不,我不知道”?你以前在做什么?
BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(stream));
    StringBuffer buffer = new StringBuffer();
    int read;
    char[] chars = new char[1024];
    while ((read = reader.read(chars)) != -1)
        buffer.append(chars, 0, read);

    return buffer.toString();
} finally {
    if (reader != null)
        reader.close();
}