-4

你好朋友我是安卓新手。我正在尝试执行json,但我不知道如何解析这个json响应..请帮助我

提前致谢 。

json响应:

{“0”:{“id”:“24”,“booking_id”:“08140001”,“user_id”:“20140620015800-127001OxL72Vdk5j”,“order_id”:“4884c1fc-2a82-11e4-a5bf-22000a989249”,“积分":"340", "want_to_say":"", "reason":"业务", "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", "国家”:“IN”,“州”:“古吉拉特邦”,“地址”:“艾哈迈达巴德”,“城市”:“艾哈迈达巴德”,“zip_code":"123456", "payment":"0", "ddate_time":"2014-08-23 06:59:27", "credit_card_number":null, "cvv":null, "expiry_date":null, "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",“电话”:“1234567980”,“手机”:“1234567890”,“国家”:“IN”,“州”:“古吉拉特邦”,“地址”:“艾哈迈达巴德”,“城市”:“艾哈迈达巴德”,“邮政编码” ":"123456", "payment":"0", "ddate_time":"2014-08-23 06:59:27", "credit_card_number":null, "cvv":null, "expiry_date":null, " language_id":"1" }}2014-08-23 06:59:27”,“credit_card_number”:null,“cvv”:null,“expiry_date”:null,“language_id”:“1”}}2014-08-23 06:59:27”,“credit_card_number”:null,“cvv”:null,“expiry_date”:null,“language_id”:“1”}}

4

3 回答 3

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();
            }

        }
于 2015-06-03T13:25:44.160 回答
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 元素具有 anull或空值,即使它们int在您的YourObject类中主要是 a,最好将它们声明为String避免java.lang.NumberFormatException.

于 2015-06-03T17:57:08.630 回答
0

使用 gson(来自 google)库。从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);
于 2015-06-03T18:58:21.110 回答