2

I have a URL request like this:

http://localhost:8080:_dc=1367504213107&filter[0][field]=site&filter[0][data][type]=string&filter[0][data][value]=test&filter[1][field]=address&filter[1][data][type]=string&filter[1][data][value]=Columbus

This is the URL request that I get from browser.

From this URL, I need to get the filter related data as a JSON object.

Basically I have filter parameters like these in the requested URL:

filter[0][field]=site
filter[0][data][type]=string
filter[0][data][value]=test
filter[1][field]=address
filter[1][data][type]=string
filter[1][data][value]=Columbus

I am using the Spring MVC framework.

1
  • 3
    That doesn't look like JSON. It looks like you are passing a list of maps of maps. Anyway, you should be able to either use a ModelAttribute, or simply use RequestParams to grab them. I am not certain how RequestParams would handle the subscripts, though. Commented May 2, 2013 at 19:09

2 Answers 2

2

Those URL parameters aren't in JSON format. I'm not sure what your question / problem is here... can't you just read in each of those parameters from the URL and then parse the data out of the strings as you need? You could take it as a single parameter and tokenize based on a custom character, or you could just loop through all the parameters and parse each one and add them to an array.

Are you saying that you need to return the data in JSON format? Get your data using the parsed parameters as I described above, then when you have the data merely serialize that object and pass it back over the wire as the body of the response. You'll want to use a JSON serialization library like Jackson which will write the object to a string in JSON for you.

Sign up to request clarification or add additional context in comments.

Comments

1

This is what that data would probably look like if it were written in JSON:

{
    "filter": 
    [
        { 
            "field": "site",
            "data": 
            { 
                "type": "string",
                "value": "test"
            }
        },
        { 
            "field": "address",
            "data": 
            { 
                "type": "string",
                "value": "Columbus"
            }
        }
    ]
}

Java code that translates from the above JSON to your-format:

JSONObject root = new JSONObject(json);
JSONArray filters = root.getJSONArray("filter");
for (int i = 0; i < filters.length(); i++)
{
    JSONObject filter = filters.getJSONObject(i);
    String field = filter.getString("field");
    JSONObject data = filter.getJSONObject("data");
    String dataType = data.getString("type");
    String dataValue = data.getString("value");
    System.out.println("filter[" + i + "][field]=" + field);
    System.out.println("filter[" + i + "][data][type]=" + dataType);
    System.out.println("filter[" + i + "][data][value]=" + dataValue);
} 

Your data format does not appear to be standard, so you will have to parse it yourself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.