我正在尝试JSON Array
用未知数据解析 a 。JSON
像这样通过
{
"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
}
现在因为在收到数据之前我不知道数据是什么,所以我不能使用传统的方法,比如
for (int i = 0; i < info.length(); i++) {
JSONObject c = info.getJSONObject(i);
String store = c.getString("Store #");
}
所以我正在尝试使用这种代码方法,但我没有成功
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));
}
}
然而,我的日志向我显示,不仅数据没有被拆分并放入ArrayLists
我希望它保持JSON
形式的状态。这是日志猫
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)
那么我做错了什么?如果您需要更多代码询问,我会发布它。谢谢您的帮助
编辑
根据我收到的答案,我对代码进行了修改。现在看起来像这样
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);
}
}
但是,正如logcat显示的那样,它现在只存储JSON
my的第一行ArrayLists
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?"