2

I want to convert this php to jsp script.. since i don't know php , i cant do it.. Please guide me..

The PHP creates a JSON object from associative array... But java have no associatve arrays , i have trouble from converting it.

$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
/*
    $response->rows[$i]['id']=$row[id];
    $response->rows[$i]['cell']=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
*/
    $response->rows[$i]['id']=$row['id'];
    $response->rows[$i]['name']=$row['name'];
    $response->rows[$i]['author']=$row['author'];
    //$response->rows[$i]=array($row[id],$row[invdate],$row[name],$row[amount],$row[tax],$row[total],$row[note]);
    $i++;
    echo json_encode($response);

The above code is in PHP. I have to convert to PHP.. This is the tried code..

My Code

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class MapToJSONExample {


ObjectMapper objectMapper = new ObjectMapper();


try {
int i=0;
.....
.....
.....

Map<String, Object> mapObject = new HashMap<String, Object>();
// page,total,records are string var.

mapObject.put("page", page);
mapObject.put("total", total);
mapObject.put("records", records);
List<Object> myList[count]  = new ArrayList<Object>();

while (rs.next())
{

myList[i].add(rs.getString("id");
myList[i].add("name");
myList[i].add("author"); 

mapObject.put("rows", myList[i]);  

} 



objectMapper.writeValue(out);

} catch (JsonGenerationException e) {
e.printStackTrace();

Is it right ? Pls help me

Ever Think
  • 683
  • 8
  • 22
  • @Bryan Do above code work ? – Ever Think Mar 22 '14 at 15:06
  • Nevermind you are using a map, what's the problem? – Bryan Mar 22 '14 at 15:06
  • @Bryan $response->rows[$i]['id']=$row['id']; $response->rows[$i]['name']=$row['name']; $response->rows[$i]['author']=$row['author']; .. How to convert this to jsp – Ever Think Mar 22 '14 at 16:07
  • where are you getting the $rows from in java? It's coming from a DB query in the php. – Bryan Mar 22 '14 at 17:01
  • @Bryan .. I want to convert associative array of php to java with similar form – Ever Think Mar 22 '14 at 17:08
  • in the second half use a map instead of a list. Like you did above. This looks pretty similar and might help you. http://stackoverflow.com/questions/18326386/java-hashmap-associative-multi-dimensional-array-can-not-create-or-add-elements – Bryan Mar 22 '14 at 21:25
  • If you don't know PHP but JSP then forget about the PHP implementation details. Take the high level requirements / use cases and implement it the JSP/Java way. – vanje Mar 23 '14 at 09:36

0 Answers0