Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a jString like this:

[{"name":"Sergio Seiji de Souza Shimose"},{"name":"Marcelo Camargo"},...]

and when I do

JSONObject jObject = new JSONObject(jstring);


I get a JSON Exception that string cannot be converted to json object.

Any help?

EDIT: That jstring is returned from a PHP web service:

PHP
function listar($username)
{
    $result = mysql_query("SELECT Name FROM APKs WHERE Tipo=0 LIMIT 5");
    $numero = 0;
    while($registo = mysql_fetch_array($result))
    {
        $regs[] = array('name' => $registo["Name"]);
        $numero++;
    }
    return json_encode($regs);
}
Posted
Updated 15-Jul-11 3:07am
v2

That one is easy: JSONObject constructor taking a string expects a JSON string which always starts with { and ends with }. You'll have to wrap your array into { and } like this for instance:

Java
string jsonString("{ "myArray" : [{"name":"Sergio Seiji de Souza Shimose"},{"name":"Marcelo Camargo"},...]}");
// Now the call should succeed
JSONObject jo = new JSONObject(jsonString);


[Edit]
Java
JSONArray array = new JSONArray(jsonString);
[/Edit]

Cheers!

—MRB
 
Share this answer
 
v2
Comments
Maxdd 7 15-Jul-11 9:08am    
Yes I supposed that, however the problem is the jstring is sent from a PHP web service

$regs[] = array('name' => $registo["Name"]);
return json_encode($regs);

See my updated question for more details...

Any change on this situation to make this work? :)
Manfred Rudolf Bihy 15-Jul-11 9:28am    
You'd have to either look into the PHP implementation of json_encode or check in your java application if the string starts and ends with {}.
Manfred Rudolf Bihy 15-Jul-11 9:36am    
I just saw that there is a corresponding JSONArray object in java. When you can make sure that the response will be an array in JSON notation or by checking for [ at the start and for ] at the end of your string, you can use JSONArray. See update!
Maxdd 7 15-Jul-11 11:40am    
It works. Thank you very much.
Manfred Rudolf Bihy 15-Jul-11 12:08pm    
Great! So you are now using JSONArray?
Guys I need your help also in similar situation :
String jsonString =new String("{");
                jsonString= jsonString + "\"";
                jsonString= jsonString +"myArray\":";
                jsonString= jsonString +returnString;
                jsonString= jsonString +"}";
                Dialog.alert(jsonString);//[{"cnt":"1","username":"tamer"}]
             // Now the call should succeed

                   JSONObject myjson = new JSONObject(jsonString);

                   JSONArray nameArray = myjson.names();
                  JSONArray valArray = myjson.toJSONArray(nameArray);
                  int size = valArray.length();
                  for(int i=0;i<valarray.length();i++)>
                  {
                      String p = nameArray.getString(i) + "," + valArray.getString(i);
                      Dialog.alert(p);
                  }



I want to access for example the value of nameArray["cnt"] while looping ... how to do that ???
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900