Reputation: 3299
I'm trying to divide this JSON string into individual strings of each contact item such as emailAddress & phone number. I'm using System.out.print to see if the information is being stored in the strings as it should. Nothing is showing up in the system.out and all that happens is what you see below in the logcat.
JSON string:
{"items":[{"description":"payment confirmed","emailAddress":"[email protected]","id":"Fri Aug 30 17:20:35 EDT 2013","nameFirst":"Bill","nameLast":"Sanders","phone":"5555555555","state":"KS","streetAddress":"123 Rick Dr","zipCode":"44444","kind":"contactinfoendpoint#resourcesItem"},{"description":"payment confirmed","emailAddress":"[email protected]","id":"Fri Aug 30 17:21:37 EDT 2013","nameFirst":"Frank","nameLast":"Lloyd","phone":"5554567896","state":"KY","streetAddress":"999 Rock St","zipCode":"44555","kind":"contactinfoendpoint#resourcesItem"}],"kind":"contactinfoendpoint#resources","etag":"\"NiDXI3T89oRrAPGJAhgGQje1Z0w/z0W4p01mjHc5cNgcOt3Kb_1xo8E\""}
Here is the logcat.
09-03 14:51:18.755: W/System.err(27991): org.json.JSONException: No value for emailAddress
09-03 14:51:18.755: W/System.err(27991): at org.json.JSONObject.get(JSONObject.java:354)
09-03 14:51:18.755: W/System.err(27991): at org.json.JSONObject.getString(JSONObject.java:510)
09-03 14:51:18.755: W/System.err(27991): at com.indeeditis.FinderActivity$EndpointsTask.doInBackground(FinderActivity.java:112)
09-03 14:51:18.765: W/System.err(27991): at com.indeeditis.FinderActivity$EndpointsTask.doInBackground(FinderActivity.java:1)
09-03 14:51:18.765: W/System.err(27991): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-03 14:51:18.765: W/System.err(27991): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-03 14:51:18.765: W/System.err(27991): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-03 14:51:18.765: W/System.err(27991): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-03 14:51:18.765: W/System.err(27991): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-03 14:51:18.765: W/System.err(27991): at java.lang.Thread.run(Thread.java:856)
My program:
public class FinderActivity extends ListActivity {
private static final String TAG_ID = "id";
private static final String TAG_FIRSTNAME = "nameFirst";
private static final String TAG_LASTNAME = "nameLast";
private static final String TAG_EMAIL = "emailAddress";
private static final String TAG_ADDRESS = "streetAddress";
private static final String TAG_STATE = "state";
private static final String TAG_PHONE = "phone";
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new EndpointsTask().execute(getApplicationContext());
}
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
public Long doInBackground(Context... contexts) {
Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
String apples = endpoint.listContactInfo().execute().toString();
JSONObject jObject = new JSONObject(apples);
JSONArray jsonArr = jObject.getJSONArray("items");
for(int i =0 ; i<jsonArr.length() ;i++ ){
JSONObject jsonObj1 = jsonArr.getJSONObject(i);
// Storing each json item in variable
String id = jsonObj1.getString(TAG_ID);
String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
String phone1 = jsonObj1.getString(TAG_PHONE);
//test to see if made it to string
System.out.print(nameFirst1);
System.out.print(nameLast1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (long) 0;
}
}
}
Your tag in logcat. Before loop to verify contents of string.
09-03 15:13:57.690: D/YOUR_TAG(30179): [{"streetAddress":"123 Rick Dr","id":"Fri Aug 30 17:20:35 EDT 2013","phone":"5555555555","nameLast":"Sanders","nameFirst":"Bill","description":"payment confirmed","zipCode":"44444","state":"KS","emailAddress":"[email protected]","kind":"contactinfoendpoint#resourcesItem"},{"streetAddress":"999 Rock St","id":"Fri Aug 30 17:21:37 EDT 2013","phone":"5554567896","nameLast":"Lloyd","nameFirst":"Frank","description":"payment confirmed","zipCode":"44555","state":"KY","emailAddress":"[email protected]","kind":"contactinfoendpoint#resourcesItem"},{"id":"Sat Aug 31 12:46:33 EDT 2013","nameLast":"j","nameFirst":"j","kind":"contactinfoendpoint#resourcesItem","description":"payment confirmed"},{"id":"Sat Aug 31 14:33:03 EDT 2013","emailAddress":"c","nameLast":"b","nameFirst":"a","kind":"contactinfoendpoint#resourcesItem","description":"payment confirmed"}]
Ok I have removed the later entries from my datastore. I receive no errors now, but I don't get any of the system outs that Im looking for.
Upvotes: 1
Views: 2250
Reputation: 36289
You can use my droidQuery library) to simplify your parsing:
Object[] array = $.makeArray(jsonArr);
for (Object obj : array)
{
Map<String, ?> map = $.map((JSONObject) obj);
for (Map.Entry<String, ?> entry : map)
{
Log.d("DEBUG", String.format(Locale.US, "Object contains Key %s and value %s", entry.getKey(), entry.getValue().toString());
}
}
Upvotes: 1
Reputation: 3230
I know that an answer has already been accepted, you could consider using the GSON library as well.
IT does exactly what you want, and there is lots of documentation and examples out there, and the library is even quite well documented:
https://code.google.com/p/google-gson/
http://www.javacreed.com/simple-gson-example/
Hope this helps for your future projects.
Upvotes: 1
Reputation: 1048
As you can see you don't have any emailAddress defined for the third object. If you are not sure that you will have every attribute in the JSONs you will have to check for the key before.
if (jsonObject.has(TAG)) {
jsonObject.getString(TAG_ID);
}
Some time I make utils to make code easier, for example:
public static String getString(JSONObject object, String tag, String defString) {
if (object.has(tag))
return object.getString(tag);
else
return defString;
}
So my code will look like this:
String nameFirst1 = Utils.getString(jsonObj1, TAG_ID, ""); // your firstName default.
String nameLast1 = Utils.getString(jsonObj1, TAG_ID, null); // maybe null??
Your JSON formatted, use this link to see it formatted (it helps me a lot)
[
{
"streetAddress":"123 Rick Dr",
"id":"Fri Aug 30 17:20:35 EDT 2013",
"phone":"5555555555",
"nameLast":"Sanders",
"nameFirst":"Bill",
"description":"payment confirmed",
"zipCode":"44444",
"state":"KS",
"emailAddress":"[email protected]",
"kind":"contactinfoendpoint#resourcesItem"
},
{
"streetAddress":"999 Rock St",
"id":"Fri Aug 30 17:21:37 EDT 2013",
"phone":"5554567896",
"nameLast":"Lloyd",
"nameFirst":"Frank",
"description":"payment confirmed",
"zipCode":"44555",
"state":"KY",
"emailAddress":"[email protected]",
"kind":"contactinfoendpoint#resourcesItem"
},
{
"id":"Sat Aug 31 12:46:33 EDT 2013",
"nameLast":"j",
"nameFirst":"j",
"kind":"contactinfoendpoint#resourcesItem",
"description":"payment confirmed"
},
{
"id":"Sat Aug 31 14:33:03 EDT 2013",
"emailAddress":"c",
"nameLast":"b",
"nameFirst":"a",
"kind":"contactinfoendpoint#resourcesItem",
"description":"payment confirmed"
}
]
Upvotes: 1
Reputation: 1180
It looks like you're making assumptions about what data each object in your array will have. One of those objects does not have every piece of data and this is throwing an error. I'm guessing your error is in one of these lines:
String id = jsonObj1.getString(TAG_ID);
String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
String phone1 = jsonObj1.getString(TAG_PHONE);
Since you're not seeing anything printed, set a breakpoint and verify that each piece of data is actually there when you first enter your for loop. In addition, I would recommend doing some defensive coding and using has() to verify that the jsonObject actually has the data before you try to grab it so you don't get these stacktraces in the future. Something like
String id = "";
if (jsonObj1.has(TAG_ID)) {
id = jsonObj1.getString(TAG_ID);
} else {
///Handle bad data
}
Upvotes: 1