0

自動生成されたテーブル名と列名を持つ 2 つのテーブルから、アセスメントの質問と回答のすべての値を取得しています。それらは .csv ファイルでサーバー側に作成されます。かなり悪いですが、 のSELECT *ステートメントを使用する方法を見つけましたPHP。とにかく、私のPHPファイルには、質問と回答の2つの配列があります。私はそれらを組み合わせて、JSON Arrayこのようなものを作ります

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形を保っています。何か案は?

4

3 に答える 3

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 として解析します

于 2013-06-28T02:11:38.973 に答える
0

問題は、JSON配列を2回ではなく1回だけ解析することだと思います

質問 - 回答リストを解析するには、JSON 配列を 2 回解析する必要があります

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));
            }
        }
    }
于 2013-06-28T06:51:06.153 に答える