GSON throwing “Expected Expected a name but was NUMBER at line 1 column 8”?

后端 未结 3 1687
心在旅途
心在旅途 2021-01-11 23:11

I\'m trying to parse a JSON string like this one ( generated URL with http://www.json-generator.com)

{
\"total\": 86, 
\"jsonrpc\": \"2.0\", 
\"id\": 1, 
\         


        
3条回答
  •  抹茶落季
    2021-01-11 23:38

    I am also face the same problem. I found that, issue was every if condition I am getting the jsonReader.nextName(). It is not correct way. I think your also doing the same way.

    From your code these two lines create the problem.

      // here the jsonReader moving one step.
      Log.d("nextName", jsonReader.nextName());
    
      //again your trying to getting the "jsonReader.nextName()" it is wrong in the above line already you got the name and reader move to next line. So  Exception may be comes here.                                    
       String name = jsonReader.nextName();   
    

    we should use "jsonReader.nextName()" one time only for each iteration in while loop like

     while (reader.hasNext())
     {
    
                String name=reader.nextName();
                if(name.equals(Constants.AUTH)){
                    authDetails.setAuth(reader.nextString());
    
                }else if(name.equals(Constants.STATUS)){
                    authDetails.setStatus(reader.nextInt());
                }else if(name.equals(Constants.MESSAGE)){
                    authDetails.setMessage(reader.nextString());
                }else {
                    reader.skipValue();
                }
    
        }
    

提交回复
热议问题