我如何在android中解析这个Json对象?

时间:2015-06-03 13:12:39

标签: android json android-webservice

你好朋友,我是android新手。而我正在尝试执行json但我不知道如何解析这个json的反应..请帮帮我

提前感谢。

json回应:

{ “0”:{ “ID”: “24”, “booking_id”: “08140001”, “USER_ID”: “20140620015800-127001OxL72Vdk5j” “ORDER_ID”: “4884c1fc-2a82-11e4-a5bf-22000a989249” “点”: “340”, “want_to_say”: “”, “原因”:“商务”, “checkindate”:“2014-10-30 00:00:00”, “checkoutdate”:“2014-10-31 00:00:00”, “FIRST_NAME”: “米兰”, “姓氏”:“巴特尔”, “电子邮件”:“milan@ncodetechnologies.com” “手机”: “1234567980”, “手机”: “1234567890”, “国”: “IN”, “状态”:“古吉拉特邦”, “地址”:“艾哈迈达巴德”, “城市”:“艾哈迈达巴德”, “ZIP_CODE”: “123456”, “金”: “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” “点”: “340”, “want_to_say”: “”, “原因”:“商务”, “checkindate”:“2014-10-30 00:00:00”, “checkoutdate”:“2014-10-31 00:00:00”, “FIRST_NAME”: “米兰”, “姓氏”:“巴特尔”, “电子邮件”:“milan@ncodetechnologies.com” “手机”: “1234567980”, “手机”: “1234567890”, “国”: “IN”, “状态”:“古吉拉特邦”, “地址”:“艾哈迈达巴德”, “城市”:“艾哈迈达巴德”, “ZIP_CODE”: “123456”, “金”: “0”, “ddate_time”:“2014-08-23 06:59:27”, “credit_card_number”:空, “CVV”:空, “EXPIRY_DATE”:空, “LANGUAGE_ID”: “1” }}

3 个答案:

答案 0 :(得分:0)

使用此代码将数据从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();
            }

        }

答案 1 :(得分:0)

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

这是指向项目的链接: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或空值,即使它们主要是int类中的YourObject,也是最好的将它们声明为String以避免java.lang.NumberFormatException

答案 2 :(得分:0)

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

现在你必须创建一个类,你必须声明你必须从json解析到该类的所有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);