Code example for JSONObject: Parsing JSON documents

Methods: getJSONObject, getLong, getString, isNull

0
		mJoinContactId = -1;
	} 
	 
	public static RawContact valueOf(JSONObject contact) {
		try { 
			final String uid = !contact.isNull("uid") ? contact.getString("uid") : null;
			// If we didn't get either a uid for the contact, 
			// then we can't do anything with it locally... 
			if (uid == null) {
				throw new JSONException("JSON contact missing required 'uid' field");
			} 
			 
			final String email = !contact.isNull("username") ?
					contact.getString("username") + "@facebook.com" : null;
			final String firstName = !contact.isNull("first_name") ?
					contact.getString("first_name") : null;
			final String lastName = !contact.isNull("last_name") ?
					contact.getString("last_name") : null;
			final String avatarUrl = !contact.isNull("picture") ?
					contact.getString("picture") : null;
			final String birthDay = !contact.isNull("birthday_date") ?
					contact.getString("birthday_date") : null;
			String statusMessage = null;
			long statusTimestamp = 0;
			if (!contact.isNull("status")) {
				try { 
					JSONObject status = contact.getJSONObject("status");
					statusMessage = status.getString("message");
					statusTimestamp = status.getLong("time");
				} catch (Exception e) {
					 
				} 
			} 
			final long syncState = !contact.isNull("x") ? contact.getLong("x") : 0;
			return new RawContact(0, uid, email, firstName, lastName, birthDay, statusMessage, statusTimestamp, avatarUrl, syncState);
		} catch (final Exception ex) {
			Log.i(TAG, "Error parsing JSON contact object" + ex.toString());
		} 
		return null;