2013-02-26 86 views
0

我无法使用JSON数组,我真的希望有人能够帮助我。无效值的Json数组

可以说我有一个JSON数据类,我正在向另一个活动发送“intent putextra”。

如何在将空值发送给其他活动之前更改空值?我做了几个打印到发现空值和它们是不同的,例如:

Monday : null 
Tuesday : 08:30 - 18:00 
Wednesday : 09:00 - 17:00 

**and so on.** 

问题是 - >我都JSON数据,我把它们解析到的对象,但我想之前“intent.putextra”和其他活动找到空给他们,并取代它们“关闭”

所以它看起来像

Monday : Closed 
Tuesday : 08:30 - 18:00 
Wednesday : 09:00 - 17:00 

编辑

public class LocationBased extends ListActivity{ 

// JSON Node names 
private static final String TAG_Location = "location_id"; 
private static final String TAG_Company = "company_id"; 
private static final String TAG_NAME = "name"; 
private static final String TAG_ADDRESS = "address"; 
private static final String TAG_PLACE = "place"; 
private static final String TAG_POSTAL = "postal"; 
private static final String TAG_CITY = "city"; 
private static final String TAG_MONDAY = "monday"; 
private static final String TAG_TUESDAY = "tuesday"; 
private static final String TAG_WEDNESDAY = "wednesday"; 
private static final String TAG_THURSDAY = "thursday"; 
private static final String TAG_FRIDAY = "friday"; 
private static final String TAG_SATURDAY = "saturday"; 
private static final String TAG_SUNDAY = "sunday"; 
private static final String TAG_TYPE = "type"; 
private static final String TAG_LAT = "lat"; 
private static final String TAG_LNG = "lng"; 
private static final String TAG_NOCAR = "nocar"; 
private static final String TAG = "Debug of Project"; // 

private String a; 
private String b; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    SQLiteDatabase db = openOrCreateDatabase("mydb.db", Context.MODE_PRIVATE, null); 
    db.execSQL("CREATE TABLE IF NOT EXISTS gps_kordinater (ID INTEGER PRIMARY KEY AUTOINCREMENT, Latitude REAL, Longitude REAL);"); 

    String query = "SELECT Latitude,Longitude FROM gps_kordinater WHERE Id = (SELECT MAX(Id) FROM gps_kordinater)"; 
    Cursor cursor = db.rawQuery(query, null); 
    if(cursor != null) 
    { 
    cursor.moveToFirst(); 
    a = cursor.getString(0); 
    b = cursor.getString(1); 
    } 

    String url = "http://webservice.XXX.XX/webservice/getLocationList.php?lat="+ a +"&lng="+ b +""; 

    Log.d(TAG, "Leyth URL = Lat : " + a +"  Long : " + b); 


    // now enabled if disabled = ingen support for jb aka 4.0 
    if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
    } 



    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 
    JSONParser jParser = new JSONParser(); 
    JSONArray json = jParser.getJSONFromUrl(url); 
    try { 
      for(int i = 0; i < json.length(); i++){ 
       JSONObject c = json.getJSONObject(i); 

      String location_id = c.getString(TAG_Location); 
      String company_id = c.getString(TAG_Company); 
      String name = c.getString(TAG_NAME); 
      String address = c.getString(TAG_ADDRESS); 
      String place = c.getString(TAG_PLACE); 
      String postal = c.getString(TAG_POSTAL); 
      String city = c.getString(TAG_CITY); 
      String monday = c.getString(TAG_MONDAY); 
      String tuesday = c.getString(TAG_TUESDAY); 
      String wednesday = c.getString(TAG_WEDNESDAY); 
      String thursday = c.getString(TAG_THURSDAY); 
      String friday = c.getString(TAG_FRIDAY); 
      String saturday = c.getString(TAG_SATURDAY); 
      String sunday = c.getString(TAG_SUNDAY); 
      String type = c.getString(TAG_TYPE); 
      String lat = c.getString(TAG_LAT); 
      String lng = c.getString(TAG_LNG); 
      String nocar = c.getString(TAG_NOCAR); 


     // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(TAG_Location, location_id); 
      map.put(TAG_Company, company_id); 
      map.put(TAG_NAME, name); 
      map.put(TAG_ADDRESS, address); 
      map.put(TAG_PLACE, place); 
      map.put(TAG_POSTAL, postal); 
      map.put(TAG_CITY, city); 
      map.put(TAG_MONDAY, monday); 
      map.put(TAG_TUESDAY, tuesday); 
      map.put(TAG_WEDNESDAY, wednesday); 
      map.put(TAG_THURSDAY, thursday); 
      map.put(TAG_FRIDAY, friday); 
      map.put(TAG_SATURDAY, saturday); 
      map.put(TAG_SUNDAY, sunday); 
      map.put(TAG_TYPE, type); 
      map.put(TAG_LAT, lat); 
      map.put(TAG_LNG, lng); 
      map.put(TAG_NOCAR, nocar); 

     // Log.d(TAG, "Leyth Days = Mandag : " + monday +"  Onsdag : " + wednesday); 


       // adding HashList to ArrayList 
      contactList.add(map); 
      } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 

    } 
    /** 
    * Updating parsed JSON data into ListView 
    * */ 
    ListAdapter adapter = new SimpleAdapter(this, contactList, 
      R.layout.list_item, 
      new String[] { TAG_LAT, TAG_LNG, TAG_POSTAL }, new int[] { 
        R.id.name, R.id.email, R.id.mobile }); 
    setListAdapter(adapter); 

    // selecting single ListView item 
    ListView lv = getListView(); 

    // Launching new screen on Selecting Single ListItem 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // getting values from selected ListItem 
      String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); 
      String cost = ((TextView) view.findViewById(R.id.email)).getText().toString(); 

      String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString(); 

      String mandag = ((TextView) view.findViewById(R.id.mandag)).getText().toString(); 
      String tirsdag = ((TextView) view.findViewById(R.id.tirsdag)).getText().toString(); 
      String onsdag = ((TextView) view.findViewById(R.id.onsdag)).getText().toString(); 
      String torsdag = ((TextView) view.findViewById(R.id.torsdag)).getText().toString(); 
      String fredag = ((TextView) view.findViewById(R.id.fredag)).getText().toString(); 
      String lordag = ((TextView) view.findViewById(R.id.lordag)).getText().toString(); 
      String sondag = ((TextView) view.findViewById(R.id.sondag)).getText().toString(); 




      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), dk.mitaffald.maps.MainActivity.class); 
      in.putExtra(TAG_LAT, name); 
      in.putExtra(TAG_LNG, cost); 
      in.putExtra(TAG_Company, description); 

      in.putExtra(TAG_MONDAY, mandag); 
      in.putExtra(TAG_TUESDAY, tirsdag); 
      in.putExtra(TAG_WEDNESDAY, onsdag); 
      in.putExtra(TAG_THURSDAY, torsdag); 
      in.putExtra(TAG_FRIDAY, fredag); 
      in.putExtra(TAG_SATURDAY, lordag); 
      in.putExtra(TAG_SUNDAY, sondag); 


      startActivity(in); 

     } 
    }); 
} 

}

+0

这里有什么更新的代码Mr.Me – Tirolel 2013-02-26 12:04:52

+0

检查我的回答 – Pragnani 2013-02-26 12:40:17

+0

您是否尝试过我解...? – Pragnani 2013-02-26 13:31:30

回答

0

我也痛苦过去这个问题,但我不知道这是很好的解决方案,但它为我工作。希望对你也有用。

String jsonObject_string ; 
try { 
       if (jsonObject != null) { 

        // ur stuff when not null 

       } 

      } catch (Exception e) { 
       // TODO: handle exception 
            // when null it automatic fill value 
       jsonObject_string = "Closed"; 
      } 
+0

此代码在你的字符串或对象为null后添加 – duggu 2013-02-26 12:05:18

+0

是的,但我使用数组,你认为我仍然可以使用它? – Tirolel 2013-02-26 12:08:49

+0

这段代码在完整的条件下运行至少对我来说,一旦你尝试使用这段代码。 – duggu 2013-02-26 12:10:37

0

为什么不尝试替换所有JSON字符串null作为一个字符串,解析JSON文件/对象之前?

可能的其他解决方案可能会循环检查每个对象是否为空。然后更换,对于Closed

编辑: 装入JSON对象作为字符串,然后全部更换nullClosed这样的:

String JSON = JsonObject.toString(); 
JSON.replace("null", "Closed"); 

编辑2: 添加此下面JSONArray json = jParser.getJSONFromUrl(url);

String s = json.toString(); // converts json object to string 
json.replace("null", "Closed"); // replaces null for Closed 
JSONArray json = new JSONArray(s); // converts back to json object 
+0

Iam有点困惑,你可以试试解释我吗? – Tirolel 2013-02-26 12:08:26

+0

我会为你添加一些代码 – Dediqated 2013-02-26 12:09:15

+0

非常感谢。 – Tirolel 2013-02-26 12:10:13

0

据我所知,你想用特定的字符串替换任何空字符串,说“闭”。 这没有任何与JSON待办事项,如果这是我的代码,我会做一个简单的检查,然后将这些值添加到我的意图。代码将如下所示:

  Intent in = new Intent(getApplicationContext(), dk.mitaffald.maps.MainActivity.class); 

     in.putExtra(TAG_LAT, name == null ? "Closed" : name); 
     in.putExtra(TAG_LAT, cost== null ? "Closed" : cost); 
     in.putExtra(TAG_Company, description == null ? "Closed" : description); 

     in.putExtra(TAG_MONDAY, mandag == null ? "Closed" : mandag); 
     .... 

依此类推。

name == null? “关闭”:名称; 简单的问一下,如果name为null,那么值是关闭的,否则返回名称。

它是一样的:

if (name == null){ 
in.putExtra(TAG_LAT, "Closed"); 
} else { 
in.putExtra(TAG_LAT, name); 
} 

我希望这是你要找的

+0

是啊我认为它这是我正在寻找,但我在阅读表单XML值,因为IAM从另一个XML页面读取值(textview)时有错误。我应该膨胀还是?导致其导致错误 – Tirolel 2013-02-26 12:31:18

+0

你不能从这样的另一个活动访问文本视图,你需要通过意图传递文本值到这个活动,膨胀该文本视图不会引用包含你想要的文本的文本视图 – 2013-02-26 12:35:19

+0

好吧我看到了,但我应该如何解析这另一个活动? String mandag =((TextView)findViewById(R.id.mandag))。getText()。toString(); 这里的失败正在logcat中出现 – Tirolel 2013-02-26 12:43:54

0
if(c.getString(TAG_MONDAY!=null && c.getString(TAG_MONDAY).length>0 && !(c.getString(TAG_MONDAY).equals(""))) 
{ 
String monday=c.getString(TAG_MONDAY); 
} 
else 
{ 
String monday="Closed"; 
}