Reputation: 1433
I am trying to parse a JSON Array
with unknown data. The JSON
comes through like this
{
"info": [
{
"1": "4",
"Store #": " 0560",
"How many microwave circuits did you run?": " 3",
"How many new ovens did you deliver to the store?": " 1",
"How many new racks did you deliver to the store?": " 5",
"Voltage readings on Turbo Chef 1": " 64",
"Voltage readings on Turbo Chef 2": " 54",
...
}
],
"success": 1
}
Now because I don't know what the data will be until it's received I can't use a traditional method like
for (int i = 0; i < info.length(); i++) {
JSONObject c = info.getJSONObject(i);
String store = c.getString("Store #");
}
So I am trying to use this code method and I am unsuccessful
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
info = json.getJSONArray(TAG_INFO);
for (int i = 0; i < info.length(); i++) {
if (info != null) {
clientList.add(info.get(i).toString());
}
}
for (String s : clientList) {
Log.v("CHECKING S", s);
s.split(":");
Log.v("CHECKING S SPLIT", s);
values.add(s);
Log.v("CHECKING VALUES 0", values.get(0));
mQuestions.add(values.get(0));
Log.v("CHECKING VALUES 1", values.get(1));
mAnswers.add(values.get(1));
}
}
However my log is showing me that not only is the data not being split up and put into the ArrayLists
as I would like it is staying in JSON
form. Here is the logcat
06-27 23:32:54.479: V/CHECKING S(32540): {"Were any of the steamers gas?":" yes","Voltage readings on Turbo Chef 4":" 34","Voltage readings on Turbo Chef 3":" 43","Voltage readings on Turbo Chef 2":" 54","Did you label all the outlets?":" yes","Voltage readings on Turbo Chef 1":" 64","How many new ovens did you deliver to the store?":" 1","If yes, did you cap the water lines?":" yes","Phone #":" (740) 389-1174","Has all new equipment been installed & have you confirmed it is all working properly?":" yes","How many new racks did you deliver to the store?":" 5","Are all oven circuits tied into electrical shut down for hood?":" yes","How many Back steamers did you remove?":" none","Date":" 6-24-13","Zip":" 43302","How many oven circuits did you run?":" 2","How many microwave circuits did you run?":" 3","If yes, did you cap the gas lines?":" yes","Did you remove the existing FRONT steamers?":" yes","Did you remove the existing BACK steamers?":" no","Voltage readings on microwave circuit 1":" 57","City":" Marion","Voltage readings on microwave circuit 3":" 92","If yes, how? Shunt Tripp or Contactor":" shunt tripp","Voltage readings on microwave circuit 2":" 87","How many front steamers did you remove?":" 2","1":"4","State":" OH","Store #":" 0560","How many existing steamers did you remove for disposal off-site?":" none","Address":" 1318 Mount Vernon Avenue","Tech Name":" Jon Doe"}
06-27 23:32:54.479: V/CHECKING S SPLIT(32540): {"Were any of the steamers gas?":" yes","Voltage readings on Turbo Chef 4":" 34","Voltage readings on Turbo Chef 3":" 43","Voltage readings on Turbo Chef 2":" 54","Did you label all the outlets?":" yes","Voltage readings on Turbo Chef 1":" 64","How many new ovens did you deliver to the store?":" 1","If yes, did you cap the water lines?":" yes","Phone #":" (740) 389-1174","Has all new equipment been installed & have you confirmed it is all working properly?":" yes","How many new racks did you deliver to the store?":" 5","Are all oven circuits tied into electrical shut down for hood?":" yes","How many Back steamers did you remove?":" none","Date":" 6-24-13","Zip":" 43302","How many oven circuits did you run?":" 2","How many microwave circuits did you run?":" 3","If yes, did you cap the gas lines?":" yes","Did you remove the existing FRONT steamers?":" yes","Did you remove the existing BACK steamers?":" no","Voltage readings on microwave circuit 1":" 57","City":" Marion","Voltage readings on microwave circuit 3":" 92","If yes, how? Shunt Tripp or Contactor":" shunt tripp","Voltage readings on microwave circuit 2":" 87","How many front steamers did you remove?":" 2","1":"4","State":" OH","Store #":" 0560","How many existing steamers did you remove for disposal off-site?":" none","Address":" 1318 Mount Vernon Avenue","Tech Name":" Jon Doe"}
06-27 23:32:54.479: V/CHECKING VALUES 0(32540): {"Were any of the steamers gas?":" yes","Voltage readings on Turbo Chef 4":" 34","Voltage readings on Turbo Chef 3":" 43","Voltage readings on Turbo Chef 2":" 54","Did you label all the outlets?":" yes","Voltage readings on Turbo Chef 1":" 64","How many new ovens did you deliver to the store?":" 1","If yes, did you cap the water lines?":" yes","Phone #":" (740) 389-1174","Has all new equipment been installed & have you confirmed it is all working properly?":" yes","How many new racks did you deliver to the store?":" 5","Are all oven circuits tied into electrical shut down for hood?":" yes","How many Back steamers did you remove?":" none","Date":" 6-24-13","Zip":" 43302","How many oven circuits did you run?":" 2","How many microwave circuits did you run?":" 3","If yes, did you cap the gas lines?":" yes","Did you remove the existing FRONT steamers?":" yes","Did you remove the existing BACK steamers?":" no","Voltage readings on microwave circuit 1":" 57","City":" Marion","Voltage readings on microwave circuit 3":" 92","If yes, how? Shunt Tripp or Contactor":" shunt tripp","Voltage readings on microwave circuit 2":" 87","How many front steamers did you remove?":" 2","1":"4","State":" OH","Store #":" 0560","How many existing steamers did you remove for disposal off-site?":" none","Address":" 1318 Mount Vernon Avenue","Tech Name":" Jon Doe"}
06-27 23:32:54.479: W/System.err(32540): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
06-27 23:32:54.479: W/System.err(32540): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
06-27 23:32:54.479: W/System.err(32540): at java.util.ArrayList.get(ArrayList.java:304)
06-27 23:32:54.489: W/System.err(32540): at com.facilitysolutionsinc.trackflex.Client$Load.doInBackground(Client.java:131)
06-27 23:32:54.489: W/System.err(32540): at com.facilitysolutionsinc.trackflex.Client$Load.doInBackground(Client.java:1)
06-27 23:32:54.489: W/System.err(32540): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-27 23:32:54.489: W/System.err(32540): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-27 23:32:54.489: W/System.err(32540): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-27 23:32:54.489: W/System.err(32540): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-27 23:32:54.489: W/System.err(32540): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-27 23:32:54.489: W/System.err(32540): at java.lang.Thread.run(Thread.java:856)
So what am I doing wrong? If you need anymore code ask, I'll post it. Thanks for the help
EDIT
Based on the answer I received I made a revision to the code. It now looks like this
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
info = json.getJSONArray(TAG_INFO);
for (int i = 0; i < info.length(); i++) {
if (info != null) {
clientList.add(info.get(i).toString());
}
}
for (String s : clientList) {
Log.v("CHECKING S", s);
String[] str_arr= s.split("[:,]");
Log.v("CHECKING VALUES 0", str_arr[0]);
mQuestions.add(str_arr[0]);
Log.v("CHECKING VALUES 1", str_arr[1]);
}
for (String content : mQuestions) {
Log.v("QUESTIONS", content);
}
}
However it's only storing the first line of my JSON
in my ArrayLists
now as the logcat shows
06-28 00:19:10.679: V/CHECKING VALUES 0(1724): {"Were any of the steamers gas?"
06-28 00:19:10.679: V/CHECKING VALUES 1(1724): " yes"
06-28 00:19:10.679: V/QUESTIONS(1724): {"Were any of the steamers gas?"
Upvotes: 0
Views: 174
Reputation: 6621
Hi I have done this for you. I am just pasting the complete code below. Try it for this kind of json and it will definitely work for you.
I call the method as :
String data = "{'info': [{'1': '4','Store #': ' 0560','How many microwave circuits did you run?': ' 3','How many new ovens did you deliver to the store?': ' 1','How many new racks did you deliver to the store?': ' 5','Voltage readings on Turbo Chef 1': ' 64','Voltage readings on Turbo Chef 2': ' 54'}],'success': 1}";
try {
JSONObject jsonData = new JSONObject(data);
populateFromResponse(jsonData);
} catch (JSONException e) {
System.out.println(" Exception occured" + e.getMessage());
e.printStackTrace();
}
}
now the implementation of populateFromResponse() method is given as:
private static final String TAG_SUCCESS = "success"; private static final String TAG_INFO = "info"; private ArrayList<JSONObject> clientList; private ArrayList<String> mQuestions; private ArrayList<String> mAnswers;
public void populateFromResponse(JSONObject json) throws JSONException {
// parse the response if (json != null) {
// Checking for SUCCESS TAG int success = json.getInt(TAG_SUCCESS); // checking for null if (success == 1) { JSONArray info = json.getJSONArray(TAG_INFO); if (info != null) {
for (int i = 0; i < info.length(); i++) {
// checking for null
if (info.get(i) != null
&& info.get(i) instanceof JSONObject)
if (clientList == null) {
clientList = new ArrayList<JSONObject>();
}
clientList.add(info.getJSONObject(i));
} }
for (JSONObject s : clientList) {
Log.v("CHECKING S", s.toString());
// Iterator containing all the keys
Iterator<String> iterator = s.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();
String value = s.getString(key);
print("CHECKING VALUES 0: " + key);
// checking for null
if (mQuestions == null) {
mQuestions = new ArrayList<String>();
}
mQuestions.add(key);
print("CHECKING VALUES 1: " + value);
// checking for null
if (mAnswers == null) {
mAnswers = new ArrayList<String>();
}
mAnswers.add(value);
}
print("CHECKING S SPLIT: " + s);
} } } else { print("response is null"); }
}
private void print(String string) { System.out.println("######-- " + string + " -----#########"); }
Output is coming as :
01-04 19:38:20.332: I/System.out(7685):
######-- CHECKING S SPLIT: {"1":"4","Voltage readings on Turbo Chef 2":" 54","Voltage readings on Turbo Chef 1":" 64","How many microwave circuits did you run?":" 3","Store #":" 0560","How many new racks did you deliver to the store?":" 5","How many new ovens did you deliver to the store?":" 1"} -----#########
01-04 19:38:20.332: I/System.out(7685): ######-- CHECKING VALUES 0: {"1" -----#########
01-04 19:38:20.332: I/System.out(7685): ######-- CHECKING VALUES 1: "4","Voltage readings on Turbo Chef 2" -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 0: 1 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 1: 4 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 0: Voltage readings on Turbo Chef 2 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 1: 54 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 0: Voltage readings on Turbo Chef 1 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 1: 64 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 0: How many microwave circuits did you run? -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 1: 3 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 0: Store # -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 1: 0560 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 0: How many new racks did you deliver to the store? -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 1: 5 -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 0: How many new ovens did you deliver to the store? -----#########
01-04 19:52:32.362: I/System.out(7841): ######-- CHECKING VALUES 1: 1 -----#########
01-04 19:52:32.363: I/System.out(7841): ######-- CHECKING S SPLIT: {"1":"4","Voltage readings on Turbo Chef 2":" 54","Voltage readings on Turbo Chef 1":" 64","How many microwave circuits did you run?":" 3","Store #":" 0560","How many new racks did you deliver to the store?":" 5","How many new ovens did you deliver to the store?":" 1"} -----#########
Now enjoy the code and be happy. bye.
Upvotes: 4
Reputation: 132982
you will need to assign s.split(":")
results to String Array as:
for (String s : clientList) {
Log.v("CHECKING S", s);
String[] str_arr= s.split(":"); //<<<<<
Log.v("CHECKING S SPLIT", str_arr);
Log.v("CHECKING VALUES 0", str_arr[0]);
mQuestions.add(str_arr[0]);
Log.v("CHECKING VALUES 1", str_arr[1]);
mAnswers.add(str_arr[1]);
}
Upvotes: 1