通过JSON的答复从web服务的变量 [英] passing json reply from webservice to variables

查看:166
本文介绍了通过JSON的答复从web服务的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴一些数据到Web服务,我得到我想要传递给一个JSONObject一个JSON答复。

Web服务的答复是:

  {
    ValidateLoginResult:[
        {
            的ErrorMessage:错误的用户名通行证,
            PROPERTYNAME:空
        }
    ]
}

和我要传递的错误信息和属性名称变量。我尝试使用的JSONObject和JSONarray但没有任何运气

 的HttpClient HttpClient的=新DefaultHttpClient();                // 2.化妆POST请求到指定的URL
                HttpPost httpPost =新HttpPost(serverURL使用);
                StringEntity本身=新StringEntity(数据);
                httpPost.setEntity(SE);                // 7.设置一些头通知服务器有关内容的类型
                httpPost.setHeader(接受,应用/ JSON);
                httpPost.setHeader(内容类型,应用/ JSON);                // 8.执行POST请求到指定的URL
                HTT presponse HTT presponse = httpclient.execute(httpPost);
                // 9.获得回复
                的InputStream = HTT presponse.getEntity()的getContent()。
                ...
                ...
                 JSONArray JSON =新JSONArray(convertInputStreamToString(的InputStream));                的JSONObject json_LL = json.getJSONObject(0);                串str_value = json_LL.getString(的ErrorMessage);


解决方案

您也越来越在的JSONObject 的反应和你想获得它在 JSONArray ..这就是为什么你得到错误。

试试这个方法...

  {尝试
    JSONObject的结果=新的JSONObject(响应);    如果(data.has(ValidateLoginResult){
        JSONArray阵列= result.getJSONArray(ValidateLoginResult);        的for(int i = 0; I< array.length();我++){
        JSONObject的OBJ = array.getJSONObject(I)
            串的ErrorMessage =+ obj.getString(的ErrorMessage);
            字符串属性名=+ obj.getString(属性名);
        }
    }}赶上(JSONException E){
    e.printStackTrace();
}

如果你想一行answer..Try这个..

  //直接到数组对象..
的JSONObject结果=新的JSONObject(响应).getJSONArray(ValidateLoginResult)getJSONObject(0)。串的ErrorMessage =+ result.getString(的ErrorMessage);
字符串属性名=+ result.getString(属性名);

i'm posting some data to a web service and i'm getting a json reply which i want to pass to a jsonobject.

The reply of the web service is:

{
    "ValidateLoginResult": [
        {
            "ErrorMessage": "Wrong username pass",
            "PropertyName": null
        }
    ]
}

and i want to pass the error message and the property name to variables. I tried using JSONobject and JSONarray but didnt have any luck

                HttpClient httpclient = new DefaultHttpClient();

                // 2. make POST request to the given URL
                HttpPost httpPost = new HttpPost(serverURL);
                StringEntity se = new StringEntity(data);
                httpPost.setEntity(se);

                // 7. Set some headers to inform server about the type of the content
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");

                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPost);
                // 9. Getting Reply
                inputStream = httpResponse.getEntity().getContent();
                ...
                ...
                 JSONArray json = new JSONArray(convertInputStreamToString(inputStream));



                JSONObject json_LL = json.getJSONObject(0);

                String str_value=json_LL.getString("ErrorMessage");

解决方案

You are getting response in JSONObject and you are trying to get it in JSONArray.. thats why you are getting error..

Try this way...

try {
    JSONObject result = new JSONObject(response);

    if(data.has("ValidateLoginResult"){
        JSONArray array = result.getJSONArray("ValidateLoginResult");

        for (int i = 0; i < array.length(); i++) {
        JSONObject obj = array.getJSONObject(i);
            String ErrorMessage= ""+obj.getString("ErrorMessage");
            String PropertyName= ""+obj.getString("PropertyName");
        }
    }

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

OR

if you want one line answer..Try this..

// going directly to array object..
JSONObject result = new JSONObject(response).getJSONArray("ValidateLoginResult").getJSONObject(0);

String ErrorMessage= ""+result.getString("ErrorMessage");
String PropertyName= ""+result.getString("PropertyName");

这篇关于通过JSON的答复从web服务的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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