Skip to content Skip to sidebar Skip to footer

Problems Parsing Json Data

I have searched for solutions for a day and cannot find one that applies for my situation. I am sorry but I am new to JSON (self taught programmer) and I don't know what classes sh

Solution 1:

The php page is echoing other strings instead of json(only).

Solution 2:

I strongly recommend using Gson to parse your JSON response.

For the example above the class would look like this:

publicclassItemList {
    List<Item> items;
}

and Item would be:

publicclassItem{
    privateString ID;
    privateString Location;
    privateString Description;
    privateString Longitude;
    privateString Latitude;
    privateString Time;
}

Then, parsing becomes absolutely trivial. Put gson-2.2.4.jar in your libs folder, and then just do this:

GsonGSON=newGson();
ItemListitemList= GSON.fromJson(json_string_here, ItemList.class);

That's it. You have a first class java object parsed from JSON. Easy and powerful.

Solution 3:

This can cause problem

mysql_connect($server, $user, $pass);
    $db_found = mysql_select_db($database);
    if($db_found){
        echo"DB Found<br>";
    }else{
        echo"DB NOT Found<br>";
    }
echo"Connection Established<br>";

your service is echoing other things. It should only be echo'ing the JSON.


Instead such information could be logged
mysql_connect($server, $user, $pass);
    $db_found = mysql_select_db($database);
    if($db_found){
        error_log("DB Found<br>");
    }else{
         error_log("DB NOT Found<br>)";
    }
error_log("Connection Established<br>");

Also the header is important to have:

header("Content-Type: application/json");

Place it at the top of your script.

EDIT: I just tried your code, and I can parse the JSON without error.

Most likely your PHP server is sending XML for some reason. You need to check that script again. It could even be error reporting messing your encoded json, Log any error but only echo the JSON .

Hope this helps.

Post a Comment for "Problems Parsing Json Data"