Gson MalformedJsonException

时间:2017-04-27 06:54:40

标签: java android gson

我正在尝试为OperatorModel[]分配一个JSON字符串,但我得到了MalformedJsonException

字符串

  

[{\"简档\":\" 69c02265-ABCA-4716-8a2f-ac5d642f876a \" \" DisplayName的\":\ " baman \" \" IsAvailable \":假,\"图像\":空},{\"简档\&# 34;:\" 1f6bdd5b-c4ea-4566-bc56-ee2f58acece8 \" \" DisplayName的\":\" nayanthara \" \&# 34; IsAvailable \":假,\"图像\":空}]

OperatorModel

public class OperatorModel {
    public String ProfileId;
    public String DisplayName;
    public Boolean IsAvailable;
    public String Image;
}

方式

private void displayAllOperators(String AllOperators, String ActiveOperators){

    operatorListNew.removeAll(operatorListNew);
    Gson gson =new Gson();

    OperatorModel[] items = gson.fromJson(AllOperators, OperatorModel[].class);

    if (items.length>0){
        for (int g=0; g< items.length; g++){

        }
    }
}

错误堆栈

zupportdesk.desk.zupport.chatsystem E/UncaughtException: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 3
at com.google.gson.Gson.fromJson(Gson.java:809)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at zupportdesk.desk.zupport.chatsystem.Operators.displayAllOperators(Operators.java:161)
at zupportdesk.desk.zupport.chatsystem.Operators.access$000(Operators.java:69)
at zupportdesk.desk.zupport.chatsystem.Operators$1.onReceive(Operators.java:146)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 3
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1310)
at com.google.gson.stream.JsonReader.nextInObject(JsonReader.java:739)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:378)
at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:349)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:761) 
at com.google.gson.Gson.fromJson(Gson.java:710) 
at com.google.gson.Gson.fromJson(Gson.java:682) 
at zupportdesk.desk.zupport.chatsystem.Operators.displayAllOperators(Operators.java:161) 
at zupportdesk.desk.zupport.chatsystem.Operators.access$000(Operators.java:69) 
at zupportdesk.desk.zupport.chatsystem.Operators$1.onReceive(Operators.java:146) 
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297) 
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46) 
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

3 个答案:

答案 0 :(得分:1)

你的json字符串无效.Json字符串总是以JsonObject开头而不是JsonArray ex。{&#34; ss&#34;:[]}

请检查您的json是否有效here

有效的json如下

{
    "array_name": [{
        "ProfileId": "69c02265-abca-4716-8a2f-ac5d642f876a",
        "DisplayName": "baman",
        "IsAvailable": false,
        "Image": null
    }, {
        "ProfileId": "1f6bdd5b-c4ea-4566-bc56-ee2f58acece8",
        "DisplayName": "nayanthara",
        "IsAvailable": false,
        "Image": null
    }]
}

创建你的json Pojo类here

答案 1 :(得分:0)

此json字符串无效。

应该是这样的 [{"\"ProfileId\"":等。

答案 2 :(得分:0)

尝试更改

11110

[{\"ProfileId\":\"69c02265-abca-4716-8a2f-ac5d642f876a\",\"DisplayName\":\"baman\",\"IsAvailable\":false,\"Image\":null},{\"ProfileId\":\"1f6bdd5b-c4ea-4566-bc56-ee2f58acece8\",\"DisplayName\":\"nayanthara\",\"IsAvailable\":false,\"Image\":null}]