0

I am sending a Java ArrayList containing JSON Objects to a PHP webpage using MultipartEntityBuilder. I am using GSON to convert the ArrayList to a String so I can send the data to the PHP webpage. This all works fine. This is my Java code:

for(List<JSONObject> rows: listOfValidationReports){
            HttpPost httppost = new HttpPost(url);
            Gson gson = new Gson(); 
            String rowsString = gson.toJson(rows);
            httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
                    .create();
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            multipartEntity.addTextBody("validationReportRows", rowsString);
            httppost.setEntity(multipartEntity.build()); // Set parameter
            HttpResponse response = client.execute(httppost, httpContext);
}

This is one of the results for the rowsString variable:

[{"map":{"severity":"WARNING","offset":11902,"column":9,"length":22,"resourceUri":"test-fm.txt","lineNumber":411,"message":"Enum name should be less than 20 characters"}},{"map":{"severity":"WARNING","offset":12329,"column":9,"length":21,"resourceUri":"test-fm.txt","lineNumber":431,"message":"Enum name should be less than 20 characters"}},{"map":{"severity":"WARNING","offset":12544,"column":9,"length":39,"resourceUri":"test-fm.txt","lineNumber":441,"message":"Enum name should be less than 20 characters"}},{"map":{"severity":"WARNING","offset":12672,"column":9,"length":21,"resourceUri":"test-fm.txt","lineNumber":446,"message":"Enum name should be less than 20 characters"}},{"map":{"severity":"WARNING","offset":393,"column":3,"length":134,"resourceUri":"test-types-test@2016-06-01.txt","lineNumber":19,"message":"y extesion, version is missing"}}]

My issue is when I send this data to PHP I am not sure how to parse it and put it into a PHP structure I can work with. There's lots of answers on how to send the data but can't find any on how you actually parse it in the PHP.

Do I need to build some sort of Regex Matcher to extract each JSON Object out of the array or is there a way to convert the String back to a PHP ArrayList equivalent where I can loop through each json object in the list. Thanks

olliejjc16
  • 361
  • 5
  • 20
  • dont roll your own, use the builtin **[json_decode](http://php.net/manual/en/function.json-decode.php)** function. It will recurse the stuff you have sent, and create the appropriate objects, arrays, associative arrays, etc ... – YvesLeBorg Jul 07 '17 at 10:51
  • 2
    Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Jul 07 '17 at 10:55

0 Answers0