2014-10-26 18 views
0

我想知道如果我能改变我得到我的JSON对象的方式。改变我获取JSONObject的方式

现在,我使用,

JSONObject d = jsonarray.getJSONObject(Integer.parseInt(xyz)-1); 
String id = d.getString(TAG_ID); 
uid.setText(id); 

我解析我jsonobject从我的数据库,现在的方式,如下所示,用户输入ID和我jsonparser查找此ID,除了我想id是一个字符串,而不是一个整数。由于它搜索的值存在不规则和特殊的迹象。它需要能够解析的数字是Code-39符号/格式。它将被用于与条形码扫描交汇处..

由于getJSONObject(int)呈现在int中,我想知道你们是否知道一种方法来改变getjsonobject的类型..或者换一种方式..因为方式它解析现在我只能用常规的数字..


编辑对于代码

} 
private class JSONParse extends AsyncTask<String, String, JSONObject> { 
    private ProgressDialog pDialog; 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(DisplayMessageActivity.this); 
     pDialog.setMessage("Getting Data ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 
    @Override 
    protected JSONObject doInBackground(String... args) { 
     JSONParser jParser = new JSONParser(); 
     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 
     return json; 
    } 
    @Override 
    protected void onPostExecute(JSONObject json) { 
     pDialog.dismiss(); 
     String xyz = edt.getText().toString(); 


     try { // Getting JSON Array 


      user = json.getJSONObject(TAG_TAG); 
      JSONObject c = user.getJSONObject(xyz); 
      String id = c.getString(TAG_ID); 
      String fname = c.getString(TAG_FIRST_NAME); 
      String lname = c.getString(TAG_LAST_NAME); 
      String email = c.getString(TAG_EMAIL); 
      String ipa = c.getString(TAG_IP); 
      String country = c.getString(TAG_COUNTRY); 
      //Set JSON Data in TextView 
      uid.setText(id); 
      name1.setText(fname); 
      name2.setText(lname); 
      email1.setText(email); 
      ipaddres.setText(ipa); 
      cou.setText(country); 




public class JSONParser { 
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 
    // constructor 
    public JSONParser() { 
    } 
    public JSONObject getJSONFromUrl(String url) { 
     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     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(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 
     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 
     // return JSON String 
     return jObj; 
    } 
} 
+0

你试过用gson吗? – mapodev 2014-10-26 16:57:54

+0

你的jsonarray来自哪里以及它与你的数据库有什么关系? – kiruwka 2014-10-26 18:02:13

回答

0

您必须使用int的原因是你和一个JSONArray工作。根据定义,它使用一个基于零的增量int作为索引。要获取具有特定属性(如“id”)的项目,您必须先解析整个数组,然后遍历这些值直到找到具有所需id属性的对象。

而不是使用数组,你可以让每个项目正常JSONObject,像这样:

{ 
    "items": { 
     "-1": {}, 
     "+1": {}, 
     "00": {} 
    } 
} 

这样,每个产品的项目对象的属性,并且可以使用它的字符串ID就可以访问:

JSONObject items = json.getJSONObject("items"); 
JSONObject myItem = items.getJSONObject(xyz); 
+0

所以你说什么,当我试图解析的JSON不是作为一个数组,但作为一个对象,我可以在我的json解析示例中包含特定的字符?可以说,所有这些0-9,AZ, - (破折号),。(点),$(美元符号),/(正斜杠),+(加号),%(百分号)和空间? 你能否为我清除一些东西?即时通讯使用parseInt或parseLong函数,但由于整数不能使用奇怪的字体我试图得到,我的解析的字符串版本是什么样子?由于没有parseString方法? – Bittornont 2014-10-26 17:39:19

+0

我的建议需要更改JSON数据的结构(从数组到普通对象)。您不需要解析字符串标识符,因为在这种情况下,每个项目的“索引”将已经是一个字符串。 – 2014-10-26 17:47:19

+0

嘿,我将json数组更改为jsonobject,除了我要求使用parseInt函数的json这个事实,您是否知道我可以使用它来检查任何值并使其返回? – Bittornont 2014-10-26 19:08:07