JSON Array
不明なデータを解析しようとしています。はこの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?"