使用Android解析包含未知数据的JSON

时间:2013-06-28 01:35:38

标签: php android json

我从两个具有自动生成的表名和列名的表中提取评估的问题和答案的所有值。它们是.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形式。有什么想法吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我不知道php,但我可以使用键值

在perl中实现类似的功能

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

答案 2 :(得分: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));
            }
        }
    }