0

Here is my php code fragment for parsing.

$result=mysql_query($qry);

    $Json="{";
    for($i=1;$i<=$count;$i++)
    {
    $row = mysql_fetch_row($result)
    $newRow=setJson($i,$row);
    if($i!=$count)
    $Json=$Json.$newRow.",";
    else
    $Json=$Json.$newRow;
    }
    $Json=$Json."}";

    $response["success"]=1;
    $response["count"]=$count;
    $response["rows"]=$Json;

    echo json_encode($response);

Function for creating new row

function setJson($i,$row)
{
$setRow="\""$i."\":[".$row."]";
return $setRow;
}

I have developed this using php. I am trying to parse MySQL result to the following format. For each row in DB. I need to update it to SQLite DB in my app.

    {
    "success":1,
    "error":0,
    "rows":{
            "1":["abc","123"],
            "2":["xxx","909"],
            "3":["bcn","1bc"]
        }
}


{
  "tag": "syncMe",
  "success": 1,
  "error": 0,
  "count": "8",
  "rows": "{
\"1\":[\"Porotta\",\"22\",\"652+2\",\"veg\",\"dinner\"],
\"2\":[\"chicken curry\",\"90\",\"sdaS\",\"veg\",\"dinner\"],
\"3\":[\"Assd\",\"12\",\"looo\",\"veg\",\"dinner\"]}"
}

How can i remove the slashes in the JSON String.??

2
  • Missing } at end in JSON is a mistake or you forgot about it? Commented Feb 6, 2014 at 6:15
  • @AdamRadomski after for loop i am adding "}" with $Json variable. As $Json=$Json."}"; Commented Feb 6, 2014 at 6:17

2 Answers 2

1

pass your result to one array variable using for loop.

Then use json_encode(array_variable);

it will give proper json format..

Why you need to format yourself manually.

You can try this way .. Note: some values i hard coded..

for($i=1;$i<=$count;$i++)
{
    $Json[$i]=array("abc","123");
}
$response["success"]=1;
    $response["count"]=3;
    $response["rows"]=$Json;

var_dump( json_encode($response));
Sign up to request clarification or add additional context in comments.

5 Comments

that i tried that too but it cant give the format i needed.
i have added it with my Question..i need to make the row in DB as JSONArrays with corresponding row number with it.
what is your query..? and the row id ur getting from db..?
SELECT * from TABLE; i took number of rows and using for loop variable i am adding row id.
this helped me. so i got idea of editing my function.
0
function setJson($i,$row)
{
$itemname=$row["item_name"];
$price=$row["price"];
$contents=$row["contents"];
$type=$row["type_of_item"];
$catagory=$row["catagory"];
//$setRow[]=array("\"".$i."\"" => $row);
$setRow="\"".$i."\":[".$itemname."\",\"".$price."\",\"".$contents."\",\"".$type."\",\"".$catagory."\"]";
return $setRow;
}

This function parsed my JSON to my requirement.

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.