2013-06-28 45 views
0

我從包含自動生成的表名和列名的兩個表中拉出評估問題和答案的所有值。它們被製作成.csv文件的服務器端。這非常糟糕,但我在PHP中找到了使用SELECT *語句的方法。無論如何,在我的PHP文件中,我有兩個數組,問題和答案。我將它們合併,並作出JSON Array這樣使用Android解析使用未知數據的JSON

try { 
    $success = 1; 

    if (!empty($questions) && !empty($answers)) { 
     $combo = array_combine($questions, $answers);  
     // success 
     echo $success; 
     // echoing JSON response 
     echo json_encode($combo); 

    } else { 
     $response["success"] = 0;   
     echo json_encode($response); 
    } 
} catch (PDOException $e) { 
    die($e->getMessage()); 
} 

現在JSON出來根據需要這樣

{ 
    "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",   
    ... 
    ... 
} 

:的左側包含的問題,正確的包含了答案。就像我想要的一樣。

問題是我的應用程序將永遠知道有多少數據這JSON Array具有或將是什麼裏面。所以使用我的常規方法解析這個信息將無法正常工作。我會在正常情況下使用這樣的

class Load extends AsyncTask<String, Void, String> { 

protected void onPreExecute() { 
//progress bar etc 
.... 
} 

protected String doInBackground(String... args) { 
try { 
// Checking for SUCCESS TAG 
int success = json.getInt(TAG_SUCCESS); 

if (success == 1) { 
    Log.v("RESPONSE", "Success!"); 
    // products found: getting Array of Questions 
    questions = json.getJSONArray(TAG_QUESTIONS); 

    // looping through All Questions 
    for (int i = 0; i < questions.length(); i++) { 

     JSONObject c = questions.getJSONObject(i); 

     // Storing each JSON item in variable 
     String name = c.getString(TAG_NAME); 
     String field = c.getString(TAG_FIELD); 
     String value = c.getString(TAG_VALUE); 

     // creating new HashMap 
     HashMap<String, String> map = new HashMap<String, String>(); 

     // adding each child node to HashMap key => value 
     map.put(TAG_NAME, name); 
     map.put(TAG_FIELD, field); 
     map.put(TAG_VALUE, value); 

     infoList.add(map); 
} 
.... 

然而,這需要你設置某種標識符的標籤在PHP和/或知道什麼隔着那麼您可以告訴代碼如何解析Strings等來了

那麼你能解析JSON與未知的數據?如果是這樣,怎麼樣? 在此先感謝

編輯

我的工作是什麼,我認爲是一個解決方案,但我需要一些幫助。下面是我使用的內部doInBackground()

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) { 
      for (int j = 0; j < info.length(); j++) { 
       clientList.add(info.get(j).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));       
        } 

       } 

的代碼,但響應停留在JSON和完全不拆呢。

的log.v貌似這個

06-27 23:26:03.419: V/CHECKING S SPLIT(32233): {"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"} 

他們都這個樣子,他們沒有被分開,他們仍然在JSON形式。有任何想法嗎?

回答

2

我想你可以改變你返回的json的結構。

也許就像吹

{ 
    "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",   
    ... 
    ... 
} 

{ 
    questions: [ 
     { 
      question: "1", 
      answer: "4" 
     }, 
     { 
      question: "Store", 
      answer: "0560" 
     }, 
     { 
      question: "How many microwave circuits did you run", 
      answer: "3" 
     }, 
     { 
      question: "How many new ovens did you deliver to the store?", 
      answer: "1" 
     }, 
     { 
      question: "How many new racks did you deliver to the store?", 
      answer: "5" 
     } 
    ] 
} 

和解析JSON作爲jsonarray

+0

感謝您的回答。然而,我需要知道如何在'PHP'中做到這一點,我不這樣做。你有一種你知道的方式嗎? –

+0

檢查編輯,我想我到了一些東西 –

0

我認爲這個問題是你只解析JSON陣列一次而不是兩次

你需要解析JSON陣列第二次解析的問題 - 答案列表

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) { 
      //parse JSON Array for the second time to parse question - answer 
      JSONArray jarray = new JSONArray(info.getString(i)); 
      for (int j = 0; j < jarray.length(); j++) { 
       clientList.add(jarray.getString(j)); 
      } 
     } 
    }