0

我从两个具有自动生成的表名和列名的表中提取评估问题和答案的所有值。它们是使用 .csv 文件制作的服务器端。这很糟糕,但我找到了一种SELECT *使用PHP. 无论如何,在我的PHP文件中,我有两个数组,问题和答案。我把它们结合起来做成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 数组一次而不是两次

需要第二次解析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));
            }
        }
    }
于 2013-06-28T06:51:06.153 回答
0

我不了解 php,但我可以使用键值在 perl 中实现类似的功能

您可以在此处阅读有关 php 中键值的更多信息,希望它可以帮助您http://php.net/manual/en/language.types.array.php

于 2013-06-28T02:59:56.503 回答