如何在 android 中解析此 Json 对象?

How Can I parse this Json object in android?

朋友们大家好,我是 android 的新人。我正在尝试执行 json 但我不知道如何解析此 json 响应.. 请帮助我

提前致谢。

json 回复:

{"0":{ "id":"24", "booking_id":"08140001", "user_id":“20140620015800-127001OxL72Vdk5j”, "order_id":"4884c1fc-2a82-11e4-a5bf-22000a989249", "points":"340", "want_to_say":"", "reason":"Business", "checkindate":"2014-10-30 00:00:00", "checkoutdate":"2014-10-31 00:00:00", "first_name":"milan", "last_name":"patel", "email":"milan@ncodetechnologies.com", "phone":"1234567980", "mobile":"1234567890", "country":"IN", "state":"Gujarat", "address":"ahmedabad", "city":"Ahmedabad", "zip_code":"123456", "payment":"0", "ddate_time":"2014-08-23 06:59:27", "credit_card_number":空, "cvv":空, "expiry_date":空, "language_id":"1" }, “1”:{ "id":"24", "booking_id":"08140001", "user_id":“20140620015800-127001OxL72Vdk5j”, "order_id":"4884c1fc-2a82-11e4-a5bf-22000a989249", "points":"340", "want_to_say":"", "reason":"Business", "checkindate":"2014-10-30 00:00:00", "checkoutdate":"2014-10-31 00:00:00", "first_name":"milan", "last_name":"patel", "email":"milan@ncodetechnologies.com", "phone":"1234567980", "mobile":"1234567890", "country":"IN", "state":"Gujarat", "address":"ahmedabad", "city":"Ahmedabad", "zip_code":"123456", "payment":"0", "ddate_time":"2014-08-23 06:59:27", "credit_card_number":空, "cvv":空, "expiry_date":空, "language_id":"1" }}

使用此代码将数据从 webservcie 解析到 android

        public void getValues_From_Web(String categoryId) {
            // Create request
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            PropertyInfo pi3 = new PropertyInfo();
            pi3.setName("Webnameid");
            pi3.setValue(categoryId);
            pi3.setType(String.class);
            request.addProperty(pi3);


            // Create envelope
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            // Set output SOAP object
            envelope.setOutputSoapObject(request);
            // Create HTTP call object
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_BRANDS);

            try {
                // Invole web service
                androidHttpTransport.call(SOAP_ACTION_BRANDS, envelope);
                // Get the response
                SoapPrimitive response4 = (SoapPrimitive) envelope.getResponse();

                //Converting string to Array list
                  List1= new ArrayList<String>();
                  List2= new ArrayList<String>();


                if ((response4.toString()).contains("{")) {

                    SoapObject rep = (SoapObject) envelope.bodyIn;
                    JSONArray jr = new JSONArray(rep.getPropertyAsString(0)
                            .toString());
                    for (int i = 0; i < jr.length(); i++) 
                    {
                        JSONObject jb = (JSONObject) jr.get(i);

                        Id= jb.getString("Id");
                        NameValues = jb.getString("NameValue");

                         List1.add(Id);
                         List2.add(NameValues);
                    }


                } else {
                    StatusoutPut = response4.toString();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

您是否尝试过使用 Google 的 GSON?

这是项目的link:https://github.com/google/gson

下载页面:http://search.maven.org/#artifactdetails%7Ccom.google.code.gson%7Cgson%7C2.3.1%7Cjar

以及解析您的 JSON:

的示例代码
Gson gson = new Gson();
YourObject[] objects = gson.fromJson(jsonString, YourObject[].class);

YourObject 应该看起来像这样:

public class YourObject{
    int id;
    int booking_id;
    String user_id;
    ...
}

请注意:如果任何 Json 元素具有 null 或空值,即使它们在您的 [=12= 中主要是 int ] class,最好声明为String,避免java.lang.NumberFormatException.

使用 gson(来自 google)库。 从 https://code.google.com/p/google-gson/downloads/list?can=1 下载库。 将下载的 zip 文件放在您的 libs 文件夹中。

现在您必须创建一个 class,您必须在其中声明您必须从 json 解析到 class 的所有 json 标记。例如

Class objClass{

String id;

String booking_id;

String user_id;
...
...

String language_id;

} 

现在你要做一个gson的对象

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();

现在,在 gson.fromJson 方法中传递您的 json 字符串。

objClass parseResponse = gson.fromJson(yourJsonResponse , objClass.class);