I am using ksoap2 to consume a SOAP based web-service, and the format of the response I am getting is something like:

Java Code:
anyType{
    key1=value1;
    key2=value2;

    key3=anyType{

        key4=value4;
        key5=value5;
        key6= anyType{
                key7= anyType{
                    key8= value8;
                };
            };

    };

    key9=value9;
}
That is the JSON objects (if we assume that this is JSON) begin with anyType{ and end with }, keys and values are separated by =, and ; are field separators/statement terminators/whatever.

I tried to validate the response string using online validators but it failed. This points out that this is not a valid JSON object.

A similar example can be found here on Stackoverflow. But the accepted answer did not work for me because, well first the response string does not begin with { but with anyType{, and if I put anyType{ in the if condition, it still raises an exception next time when it encounters an anyType{ (a nested JSON object)

The second answer seems to work to some extent, but the problem is that my entire response string appears to come as a single property (since the propertyCount is 1), so when I print out the name or value of the property, the entire response string is printed.

I have googled it a lot and tried everything I could find. So I suppose I'll have to parse it myself.

My question is what is the best way to parse this kind of response.


Should I try to parse it using regex or should I convert the response string to a JSON format by replacing all occurances of anyType{ by { , = by : , ; by , etc. etc. , and then converting this string to a JSONObject by something like:

Java Code:
jsonObject= new JSONObject(responseString);
and then extract keys and values by something like:

Java Code:
Iterator itr= jsonObject.keys();

        while(itr.hasNext()) {
            String value = null; 

            String key= (String) itr.next();
            try {
                value= jsonObject.getString(key);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            Log.i(TAG, key + " : " + value); 

            // ......
        }
__________________________________________________ __________________________________________________

Actually I did try to manipulate the response string and have got to the following. I am still working on it, but this does not seem to be an efficient solution at all. There must be a better way then this!

Java Code:
import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

public class JSONPracticeOne {

    public static void main(String[] args) {
        String responseString= " anyType{key1=value1;key2=value2;key3=anyType{key4=value4;key5=value5;key6=anyType{key7=anyType{key8=value8};};};key9=value9;}";

        responseString = responseString.replaceAll("anyType\\Q{\\E", "{");

        responseString = responseString.replaceAll("\\Q=\\E", ":");

        responseString = responseString.replaceAll(";", ",");

        responseString = responseString.replaceAll(",\\Q}\\E","}");

        System.out.println(responseString); 


    }

}
OUTPUT:-

Java Code:
{
    key1:value1,
    key2:value2,
    key3:{
        key4:value4,
        key5:value5,
        key6:{
            key7:{
                key8:value8
            }
        }
    },
    key9:value9
}
So if you have a better way of parsing this kind of response, I'll really appreciate your suggestions/tips. Thank you.

Note:- I also posted it on SO but could not get a (satisfactory) response.