Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 28
Registered: ‎11-13-2012
My Device: none
My Carrier: 27768965251

JSON Object Handling

I have an JSON string with values in.

Used from an online tutorial I found.

 

But I get an error on the 'Log.i'

Error: The method i(String, String) is undefined for the type Log

 

 

Here's the script:


 

String jsonObj = new String("messages\"[{\"message\"{\"from\":\"abc\",\"to\":\"xyz\",\"msg\":\"hi! xyz\"}}{\"message\"{\"from\":\"pqr\",\"to\":\"abc\",\"msg\":\"hi! abc whats up?\"}}]");

 

JSONObject json = new JSONObject(jsonObj);
JSONArray jArray= json.getJSONArray("messages");


for(int i=0;i<jArray.length();i++){
   JSONObject j = jArray.getJSONObject(i);
   String from = j.optString("from");
   Log.i("########SENDER",from);
   String to =j.optString("to");
   Log.i("########RECEIVER",to);
   String message=j.optString("msg");
   Log.i("########MESSAGE",message);
}


 

Why would I get this error?

Developer
Posts: 16,845
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: JSON Object Handling

Log is not a class in the blackberry API, so unless it is defined in a library or in your project you won't be able to use it.
you can use sysout to log to the console, or the EventLogger class to log to the device log.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 28
Registered: ‎11-13-2012
My Device: none
My Carrier: 27768965251

Re: JSON Object Handling

that still gives me the same error

System.out.i("########SENDER",from);

 

All i'm trying to achieve is targeting the index of the JSON.

 

What would you suggest?

Developer
Posts: 16,845
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: JSON Object Handling

System.out.println(String) is the syntax, not '.i'
see http://www.blackberry.com/developers/docs/7.1.0api/java/lang/System.html
http://www.blackberry.com/developers/docs/7.1.0api/java/io/PrintStream.html#println(java.lang.String...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 28
Registered: ‎11-13-2012
My Device: none
My Carrier: 27768965251

Re: JSON Object Handling

I know what that is.
But like I said. I'm running a loop through the JSON, to get each message.....obviously I must use some sort of indexing to target and display the next message.

You aren't answering a question I asked.
How would you do it?

I know what System.out.println is
Highlighted
Developer
Posts: 16,845
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: JSON Object Handling

[ Edited ]

i know only what you write, and if you write that
System.out.i() gives you an error: Well...

What is your issue now?

Did you try

http://supportforums.blackberry.com/t5/Java-Development/Sample-Code-Implementing-JSON-in-your-applic...

?

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter