I need to decode JSONObject in Android 2.1 with Base64.I know that Base64 class supports Android 2.2+,that's why I include the source code in my project.So I need to do something like that :
JSONObject clientHash = new JSONObject();
byte[] tmpSecData = Base64.decode(clientHash.getJSONObject("client_auth_hash"));
Any suggestions how to do that or is it possible?
Lets try it,
Convert the clientHash.getJSONObject("client_auth_hash") in String then byteArray,
then use,
byte temp[];
Base64 b = new Base64();
String jsonString = clientHash.getJSONObject("client_auth_hash").toString();
temp = b.decode(jsonString.getBytes());
then use your temp byte[].
Hope this will help you. If its work then inform me. Thanx.
Related
I'm wondering what is the difference between using a JSONObject and JSONTokener to parse a JSON string.
I have seen examples where either of these classes were used to parse a JSON string.
For example, I parsed a JSON string like this:
JSONObject object = (JSONObject) new JSONTokener(jsonString).nextValue();
return object.getJSONObject("data").getJSONArray("translations").
getJSONObject(0).getString("translatedText");
But I also see examples where they simply use a JSONObject class (without using JSONTokener) to parse it:
String in;
JSONObject reader = new JSONObject(in);
JSONObject sys = reader.getJSONObject("sys");
country = sys.getString("country");
I read the description on this page, but still don't really get the difference. Is one better than the other performance wise and when should a JSONTokener be used?
Thanks!
i have byteArray.
is it possible to convert byteArray to String?
please check my code
byte[] data = **some_byte_array**
JSONObject jsonObject = new JSONObject(data);
how do i fix this.
Try this
String decoded = new String(bytes, "UTF-8");
There are a bunch of encodings you can use, look at the Charset class in the Sun javadocs.
The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.
answer credit goes to https://stackoverflow.com/a/1536365/4211264
Yes you can convert byte array to String using one of the String constructors like this :
String myString = new String(yourByteArray);
Documentation for the same:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[])
All the best :)
I'm new in android ... so I need help. I have JSON link where I have for example like this
[{"id":"82","percent":"3","image_name":"something.jpg"....
I try some tutorial just for reading text, it was okay. But I have problem how to show in my layout the images from JSON...
Can you help or share some code about this?
Parse the json string : -
String jsonString = "your json string goes here" ;
JSONObject rootObj = new JSONObject(jsonString);
String link = rootObj.getString("image_name");
You can now use this url/link to convert it into bitmap : -
Bitmap image ;
InputStream in = new java.net.URL(link).openStream();
image = BitmapFactory.decodeStream(in);
in.close();
By default you can't disaplay image using web link. However lots of libraries have been created to solve this problem. You can try this one. There's also a list of alternative libraries.
I have a problem sending Json messages with RabbitMQ using Android.
I have to parse RabbitMQ object to string and bytes to send the message
channel.basicPublish("", Cola_RPC, props, mensaje_parseado.toString().getBytes()); //Mensaje_parseado is the Json
When I recive the message I don't know how to parse into Json again to get the original message.
I try:
String stringJSON = delivery.getBody().toString();
JSONObject respuesta_desparseada = new JSONObject (stringJSON);
But it doesn't work.
Maybe is a noobie question, but I can't reach the soluction on Google.
Thank you in advance.
You need to change this:
String stringJSON = new String(delivery.getBody());
JSONObject respuesta_desparseada = new JSONObject (stringJSON);
You are calling toString() in a byte[] that uses the toString() method from Object.
If you want to see why, you can run this:
String myString = "myTest";
System.out.println(myString);
System.out.println(myString.getBytes());
System.out.println(myString.getBytes().toString());
System.out.println(new String(myString.getBytes()));
I am reading json file from assets folder in my project in android. I am trying to convert String returned after reading json file into JSONObject. I googled alot but can't find fault in my code. Others are having success using this code. Anyone, kindly guide me if my approach is wrong or some fault is in my code.
At line - //1 , i get the error stated in title.
AssetManager am = getAssets();
InputStream is;
try
{
is=am.open("places.json");
int size = is.available();
byte[] buffer = new byte[size];
is.close();
String bufferString = new String(buffer);
JSONObject jo = new JSONObject(bufferString); //1
JSONObject pjo = jo.getJSONObject("Places");
You should consider using JSONReader:
http://developer.android.com/reference/android/util/JsonReader.html
Available from API level 11.
But I'm sure it's also available via the compatibility library v4