GSON抛“预期的预期名字却被人数为行1列8”? [英] GSON throwing “Expected Expected a name but was NUMBER at line 1 column 8”?

查看:223
本文介绍了GSON抛“预期的预期名字却被人数为行1列8”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析一个JSON字符串像这样的(生成的URL与 HTTP://www.json- generator.com

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, 
"result": [
    {
        "startDate": "14/03/2012", 
        "meetingId": "1330", 
        "creator": "Jhon", 
        "lastModified": "02/04/2012", 
        "meetingTitle": "task clarification", 
        "location": "Conf hall", 
        "startTime": "02:00 PM", 
        "createdDate": "14/03/2012", 
        "owner": "Peter", 
        "endTime": "02:30 PM"
    }, 
    {
        "startDate": "20/03/2012", 
        "meetingId": "1396", 
        "creator": "Mr.Hobbs", 
        "lastModified": "07/09/2012", 
        "meetingTitle": "Design Work", 
        "location": "South conf Room", 
        "startTime": "03:30 PM", 
        "createdDate": "19/03/2012", 
        "owner": "Steve Jobs", 
        "endTime": "04:30 PM"
    }, 
    {
        "startDate": "22/03/2012", 
        "meetingId": "1432", 
        "creator": "Robin", 
        "lastModified": "21/03/2012", 
        "meetingTitle": "Do something new", 
        "location": "NA", 
        "startTime": "10:00 AM", 
        "createdDate": "21/03/2012", 
        "owner": "Mr.Bean", 
        "endTime": "11:00 AM"
    }
  ]

}

下面是我使用的一个对象类:

Here's an object class I'm using:

public class Country {

String startDate;
 String meetingId;
 String creator;
 String lastModified;
 String meetingTitle;
 String location;
 String startTime;
 String createdDate;
 String owner;
 String endTime;

 public String getStartDate() {
    return startDate;
}
public void setStartDate(String startDate) {
    this.startDate = startDate;
}
public String getMeetingId() {
    return meetingId;
}
public void setMeetingId(String meetingId) {
    this.meetingId = meetingId;
}
public String getCreator() {
    return creator;
}
public void setCreator(String creator) {
    this.creator = creator;
}
public String getLastModified() {
    return lastModified;
}
public void setLastModified(String lastModified) {
    this.lastModified = lastModified;
}
public String getMeetingTitle() {
    return meetingTitle;
}
public void setMeetingTitle(String meetingTitle) {
    this.meetingTitle = meetingTitle;
}
public String getLocation() {
    return location;
}
public void setLocation(String location) {
    this.location = location;
}
public String getStartTime() {
    return startTime;
}
public void setStartTime(String startTime) {
    this.startTime = startTime;
}
public String getCreatedDate() {
    return createdDate;
}
public void setCreatedDate(String createdDate) {
    this.createdDate = createdDate;
}
public String getOwner() {
    return owner;
}
public void setOwner(String owner) {
    this.owner = owner;
}
public String getEndTime() {
    return endTime;
}
public void setEndTime(String endTime) {
    this.endTime = endTime;
}




} 

但它抛出我,:

But it throws me with:

     W/JSONStreamReader(1153): java.lang.IllegalStateException: Expected a name but was NUMBER at line 1 column 8
08-09 01:21:37.629: W/JSONStreamReader(1153): java.lang.IllegalStateException: Expected a name but was NUMBER at line 1 column 8
08-09 01:21:37.629: W/JSONStreamReader(1153):   at com.google.gson.stream.JsonReader.nextName(JsonReader.java:785)
08-09 01:21:37.629: W/JSONStreamReader(1153):   at com.example.gsontest.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:162)

以JSONID:1,是多少!是什么原因呢?我生成JSON是 http://www.json-generator.com

in Json "id": 1, is the number! is that is the reason? I'm generating JSON with http://www.json-generator.com

我的Aysc $ C $下解析JSON:

My Aysc Code for Parsing Json:

 private class MyAsyncTask extends AsyncTask<String, Void, Void> {

        private static final int REGISTRATION_TIMEOUT = 3 * 1000;
        private static final int WAIT_TIMEOUT = 30 * 1000;
        private final HttpClient httpclient = new DefaultHttpClient();

        final HttpParams params = httpclient.getParams();
        private boolean error = false;

        protected Void doInBackground(String... urls) {

            String URL = null;
            Log.d("ConnManagerParams", "ok?");
            try {

                // URL passed to the AsyncTask
                URL = urls[0];
                HttpConnectionParams.setConnectionTimeout(params,
                        REGISTRATION_TIMEOUT);
                HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
                ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);

                Log.d("ConnManagerParams", "ok?");
                HttpPost httpPost = new HttpPost(URL);
                Log.d("httpPost", "ok?");
                // Response from the Http Request
                HttpResponse response = httpclient.execute(httpPost);
                Log.d("response", "ok?");
                // Check the Http Request for success
                StatusLine statusLine = response.getStatusLine();
                Log.d("statusLine", response.getStatusLine().toString());
                // Log.d("RESPONSE",
                // EntityUtils.toString(response.getEntity()));

                if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                    // Log.d("statusok", statusLine.getStatusCode());
                    Gson gson = new Gson();
                    // create a new JSON reader from the response input stream
                    Log.d("gson", "gson?");
                    JsonReader jsonReader = new JsonReader(
                            new InputStreamReader(response.getEntity()
                                    .getContent(), "UTF-8"));
                    // begin parsing
                    Log.d("AFTjsonReader", "AFTjsonReader?");
                    jsonReader.beginObject();
                    Log.d("beginObject", "beginObject?");
                    // stay in loop as long as there are more data elements
                    while (jsonReader.hasNext()) {
                        // get the element name
                        Log.d("whilejsonReader", "whilejsonReader?");
                        // String name = jsonReader.nextName();
                        Log.d("nextName", jsonReader.nextName());
                        String name = jsonReader.nextName();

                        Log.d("nextNametest2", "nextName?");

                        if (name.equals("result")) {

                            Log.d("result", "result?");
                            jsonReader.beginArray();

                            while (jsonReader.hasNext()) {
                                // parse every element and convert that to a
                                // country object
                                Country country = gson.fromJson(jsonReader,
                                        Country.class);
                                // add the country object to the list
                                countryList.add(country);

                            }
                            jsonReader.endArray();
                        }

                        // success = jsonReader.nextBoolean();
                        success = true;

                    }
                    // end reader and close the stream
                    jsonReader.endObject();
                    jsonReader.close();

                } else {
                    // Closes the connection.
                    Log.d("Closes the connection.", "Closes the connection.?");
                    Log.w(LOG_TAG, statusLine.getReasonPhrase());
                    response.getEntity().getContent().close();
                    throw new IOException(statusLine.getReasonPhrase());
                }

            } catch (Exception e) {
                Log.d("catch", "catch");
                Log.w(LOG_TAG, e);

                error = true;
                cancel(true);
            }

            return null;

        }

        protected void onCancelled() {
            Log.e(LOG_TAG, "Error occured during data download");
        }

        protected void onPostExecute(Void unused) {
            if (error) {
                Log.e(LOG_TAG, "Data download ended abnormally!");
            } else {
                displayCountries();
            }
        }

    }

}

我应该怎么解决这个问题的任何想法?

Any ideas how should I fix it?

谢谢!

推荐答案

如果你打算使用 JsonReader 和手工解析流,你不使用的通过 Gson.fromJson自动反序列化()

If you're going to use JsonReader and parse the stream manually, you don't use the automatic deserialization via Gson.fromJson().

您要么需要从每一个对象在遍历数组提取领域,一旦你在你的循环,或简单地使用自动反序列化与适当的类(其中,坦率地说,就是你应该做的):

You either need to extract the fields from each object while traversing that array once you're in your loop, or simply use the automatic deserialization with an appropriate class (which, honestly, is what you should be doing):

class Response {
    private int total;
    private String jsonrpc;
    private int id;
    private List<Country> result;

   // getters and setters ...
}

和后来干脆:

InputStreamReader isr = new InputStreamReader(response.getEntity()
                                .getContent(), "UTF-8"));
Response r = gson.fromJson(isr, Response.class);

这篇关于GSON抛“预期的预期名字却被人数为行1列8”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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