Android:操纵IF语句的解析JSON

时间:2011-12-22 08:22:30

标签: android database json

我正在开发一个使用sql&的android应用程序php连接到外部服务器。我能够将正确的信息检索到应用程序中,但是,我在操作已解析的JSON数据时遇到了问题

此代码成功返回我查询的数据。

try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag","venID: "+json_data.getInt("venID")
                        );
                        //Get an output to the screen
                        returnString += jArray.getJSONObject(i); 


                }
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return returnString; 
    }    

我通过将其放入textview来验证我想要返回的数据是否正确:

final TextView txt = (TextView)findViewById(R.id.textView2);
            txt.setText(getServerData(KEY_121));

并将结果显示为:

http://xxx.xxx.x.xxx/sponsorimage.php{"venID":"1"}

我想知道我如何操纵返回的信息要么结果只显示数字1,因为我试图获取该字符串并在if语句中使用它来执行另一个操作

修改

以下是此特定部分的完整代码

public static final String KEY_121 = "http://xxx.xxx.x.xxx/sponsorimage.php"; //i use my real ip here



private String getServerData(String returnString) {

   InputStream is = null;

   String result = "";
    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("sponsorImage","0"));

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(KEY_121);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }

    //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }
    //parse json data
    try{
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","venID: "+json_data.getInt("venID")
                    );
                    //Get an output to the screen
                    returnString += jArray.getJSONObject(i); 


            }
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return returnString; 
}    

2 个答案:

答案 0 :(得分:1)

如果您只想访问JSON对象的venID参数(在本例中为“1”),则可以使用JSONObject.getInt(String key)方法。您已在日志声明中使用它。

JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","venID: "+json_data.getInt("venID")

这样您就可以访问JSON对象的任何参数 http://www.json.org/javadoc/org/json/JSONObject.html

-axel

答案 1 :(得分:0)

好吧,你刚刚浏览了JSON数组并将所有对象连接成单个字符串。然后你又回来了。如果您只需要一个对象的一个​​字段,为什么要这样做?

PS:最好在字符串上使用StringBuilder而不是+ =。