Using Jsonobject I extracted data from RawmatrixData and stored it in Object:
org.json.JSONObject item = Fir.getJSONObject(i); Object value1 = item.get("RawMatrixData")`
Now i want to replace data 342771123181 with some string value, how to achieve this?
I tried with ArrayList<String> and ArrayList<ArrayList<String>>.
"LstMatrixFirmInfo": [
{
"RawMatrixData": "[[342771123181,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],[3427714486446,1,2,null,null,null,null,null,null,null,null,null,null,null,28.99,28.99,28.99,25,4.81,4.81,4.81,null,null,null,null,null,null,null,null,null]]]}
Is RawMatrixData supposed be a string like in the image or JSON Array.
If RawMatrixData is supposed to be string then maybe converted to JSONArray
I would just use string replace.
String replacedText = Fir.getString('RawMatrixData').replace('342771123181', 'foobar')
Fir.push('RawMatrixData', replacedText);
The above can be done in one line but for ease of understanding it hasn't. First line gets your string from the Json object then replace the number with foobar.
Then text is pushed back on to the json object overwriting the old value.
The above code i believe sorts out your problem based on the picture you provided.
If the RawMatrixData was supposed to be a JSON array and not a string then in that case you would have to traverse the whole array replace as you go.
Here is a sample snippet where I am trying to get array list from a json object by I am getting class cast exception while doing this.
public static void main(String args[]) throws Exception {
List<String> arrayList = new ArrayList<String>();
arrayList.add("a");
arrayList.add("b");
arrayList.add("c");
arrayList.add("d");
arrayList.add("e");
JSONObject json = new JSONObject();
json.put("raja", "suba");
json.put("arraylist", arrayList);
System.out.println("Thus the value of array list is : "+json.get("arraylist"));
List<String> oldlist = (List<String>) json.get("arraylist");
System.out.println("Old list contains : "+oldlist);
System.out.println("The old json contains : "+json.toString());
String result = json.toString();
JSONObject json1 = new JSONObject(result);
System.out.println("The new json value is : " +json1);
System.out.println("The value in json for raja is :" +json1.get("raja"));
System.out.println("The vlaue of array list is : "+json1.get("arraylist"));
List<String> newlist = (List<String>) json1.get("arraylist");
System.out.println("Thus the value of new list contains : "+newlist);
}
I am not getting exception while obtaining oldlist but I'm getting class cast exception while obtaining newlist.
Why the array list is treated as an object in the first case but as a string in the second case?
What json.toString() really does? Does it converts the entire json object to string like appending double quotes "" to entire json object or does it converts each and every object within it into string?
The problem here is that when you do
json.put("arraylist", arrayList);
you are explicitly asking for a list serialization. This means that underlying logic will be performed in order to save class-related metadata into the JSONObject. When you do json.toString(), though, this metadata are lost and you end up with a old plain String object. Then, when you try to deserialize this String at runtime, you get a ClassCastException because it doesn't really know how to convert that String into a list.
To answer your second question, JSONObject.toString() returns a String representation of an object in JSON format. This means that if you have an ArrayList, instead of having a field with the ArrayList.toString() value, you will have an array element with the list values.
i have ArrayList with multiple value. i want to convert this ArrayList into String to save in sharedPreferences, then I want to retrieve the String and convert it back to ArrayList
Please tell how to do that? (or any other idea to store and retrieve ArrayList)
Convert arraylist to string:
String str = "";
for (String s : arraylist)
{
str += s + ",";
}
Save string into sharedpreference:
PreferenceManager.getDefaultSharedPreferences(context).edit().putString("mystr", str).commit();
get string from sharedpreference:
String str = PreferenceManager.getDefaultSharedPreferences(context).getString("mystr", "defaultStringIfNothingFound");
Convert string to arraylist:
List<String> arraylist = new ArrayList<String>(Arrays.asList(str.split(",")));
You can use Google's GSON.
Gson is a Java library that can be used to convert Java Objects into
their JSON representation. It can also be used to convert a JSON
string to an equivalent Java object. Gson can work with arbitrary Java
objects including pre-existing objects that you do not have
source-code of.
http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html
https://code.google.com/p/google-gson/
You get the idea:
Store: Convert Object to JSON String -> Save string
Retrieve: Get string -> Convert from JSON to Object
I have an array of strings like [1234_acb,2345_xyz]
I want to form a key value pair JSON Object like [{"1234":"abc"},{"2345":"xyz"}]
I have used split function to separate out the values from underscore
assume your string data is stored in data:
String keyValArray[] = data.split("_");
JSONArray jsonArray = new JSONArray();
for(int a=0;a<keyValArray.length-1;a+=2){
jsonArray.put(new JSONObject().put(keyValArray[a], keyValArray[a+1]));
}//for loop
String jsonStr = jsonArray.toString();
P.S: for loop limit is keyValArray.length-1 so keyValArray[a+1] does not go out of the array bound
P.S2: #njzk2 a++ changed to a+=2, to skip the value item in array, so it's not used as a key in the json object
In my application, I need to pass JSON array to java then convert that array to java array using java as a language. Here is the code.
JavaScript
function arraytofile(rans)
{
myData.push(rans); // rans is the random number
var arr = JSON.stringify(myData);// converting to json array
android.arraytofile(arr); // passing to java
}
Java
public void arraytofile(String newData) throws JSONException {
Log.d(TAG, "MainActivity.setData()");
System.out.println(newData);
}
newData is printing data as [[2],[3],[4]]... I want in regular java array. How I can achieve this?
You can use Google gson . An excellent Java library to work with JSON.
Find it here: https://code.google.com/p/google-gson/
Using gson library classes you can do something like this:
// I took a small sample of your sample json
String json = "[[2],[3],[4]]";
JsonElement je = new JsonParser().parse(json);
JsonArray jsonArray = je.getAsJsonArray();
// I'm assuming your values are in String, you can change this if not
List<String> javaArray = new ArrayList<String>();
for(JsonElement jsonElement : jsonArray) {
JsonArray individualArray = jsonElement.getAsJsonArray();
JsonElement value = individualArray.get(0);
// Again, I'm assuming your values are in String
javaArray.add(value.getAsString());
}
Now you have your json as Java List<String>. That's basically as array.
If you know exact number of results, you can surely define an array of fix size and then assign values to it.
If you know Java, than you already know how to go from here.
I hope this helps.