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.
Related
I have the following string and I need to split it to get the two objects inside:
[Object{value1="1", value2="2"}, Object{Value1="1", value2="2"}]
You could try:
String[] splitTextObject = YOUR_STRING.split(", ");
String object1 = splitTextObject[0];
String object2 = splitTextObject[1];
...
But I don't think you actually need to split the string this way in order to achieve getting each object, and instead you should consider parsing your JSON. Perhaps utilise GSON.
I am trying get gadgetry_thumbnail_image path from JSON array with this, but its return null:
JSONObject custom_fields = postObject.getJSONObject("custom_fields");
String thumbpath = custom_fields.optString("gadgetry_thumbnail_image");
This is the JSON array:
{"posts":[{"custom_fields":{"gadgetry_post_viewed":["12"],"gadgetry_tfuse_post_options":["a:58:{s:22:\"gadgetry_disable_image\";s:5:\"false\";s:22:\"gadgetry_disable_video\";s:5:\"false\";s:26:\"gadgetry_disable_post_meta\";s:5:\"false\";s:23:\"gadgetry_disable_author\";s:5:\"false\";s:31:\"gadgetry_disable_published_date\";s:5:\"false\";s:24:\"gadgetry_disable_coments\";s:5:\"false\";s:28:\"gadgetry_disable_author_info\";s:5:\"false\";s:19:\"gadgetry_page_title\";s:13:\"default_title\";s:21:\"gadgetry_custom_title\";s:0:\"\";s:21:\"gadgetry_single_image\";s:0:\"\";s:30:\"gadgetry_single_img_dimensions\";a:2:{i:0;s:3:\"320\";i:1;s:3:\"250\";}s:28:\"gadgetry_single_img_position\";s:10:\"alignright\";s:24:\"gadgetry_thumbnail_image\";s:104:\"http:\/\/www.mysite.com\/wp-content\/uploads\/2015\/11\/11666271_761774897268291_544140090628098983_n.jpg\";s:27:\"gadgetry_thumbnail_position\";s:7:\"noalign\";s:19:\"gadgetry_video_link\";s:0:\"\";s:25:
THIS is the complete Array
{"custom_fields":{"gadgetry_post_viewed":["4"],"gadgetry_tfuse_post_options":["a:58:{s:22:\"gadgetry_disable_image\";s:5:\"false\";s:22:\"gadgetry_disable_video\";s:5:\"false\";s:26:\"gadgetry_disable_post_meta\";s:5:\"false\";s:23:\"gadgetry_disable_author\";s:5:\"false\";s:31:\"gadgetry_disable_published_date\";s:5:\"false\";s:24:\"gadgetry_disable_coments\";s:5:\"false\";s:28:\"gadgetry_disable_author_info\";s:5:\"false\";s:19:\"gadgetry_page_title\";s:13:\"default_title\";s:21:\"gadgetry_custom_title\";s:0:\"\";s:21:\"gadgetry_single_image\";s:0:\"\";s:30:\"gadgetry_single_img_dimensions\";a:2:{i:0;s:3:\"320\";i:1;s:3:\"250\";}s:28:\"gadgetry_single_img_position\";s:10:\"alignright\";s:24:\"gadgetry_thumbnail_image\";s:104:\"http:\/\/www.mystite.rs\/wp-content\/uploads\/2015\/11\/11666271_761774897268291_544140090628098983_n.jpg\";s:27:\"gadgetry_thumbnail_position\";s:7:\"noalign\";s:19:\"gadgetry_video_link\";s:0:\"\";s:25:\"gadgetry_video_dimensions\";a:2:{i:0;s:3:\"590\";i:1;s:3:\"191\";}s:23:\"gadgetry_video_position\";s:9:\"alignleft\";s:23:\"gadgetry_header_element\";s:7:\"without\";s:22:\"gadgetry_select_slider\";s:2:\"-1\";s:17:\"gadgetry_page_map\";s:0:\"\";s:25:\"gadgetry_content_ads_post\";s:4:\"true\";s:21:\"gadgetry_top_ad_space\";s:5:\"false\";s:21:\"gadgetry_top_ad_image\";s:0:\"\";s:19:\"gadgetry_top_ad_url\";s:0:\"\";s:23:\"gadgetry_top_ad_adsense\";s:0:\"\";s:28:\"gadgetry_bfcontent_ads_space\";s:5:\"false\";s:23:\"gadgetry_bfcontent_type\";s:5:\"image\";s:25:\"gadgetry_bfcontent_number\";s:3:\"one\";s:29:\"gadgetry_bfcontent_ads_image1\";s:0:\"\";s:27:\"gadgetry_bfcontent_ads_url1\";s:0:\"\";s:31:\"gadgetry_bfcontent_ads_adsense1\";s:0:\"\";s:29:\"gadgetry_bfcontent_ads_image2\";s:0:\"\";s:27:\"gadgetry_bfcontent_ads_url2\";s:0:\"\";s:31:\"gadgetry_bfcontent_ads_adsense2\";s:0:\"\";s:29:\"gadgetry_bfcontent_ads_image3\";s:0:\"\";s:27:\"gadgetry_bfcontent_ads_url3\";s:0:\"\";s:31:\"gadgetry_bfcontent_ads_adsense3\";s:0:\"\";s:29:\"gadgetry_bfcontent_ads_image4\";s:0:\"\";s:27:\"gadgetry_bfcontent_ads_url4\";s:0:\"\";s:31:\"gadgetry_bfcontent_ads_adsense4\";s:0:\"\";s:29:\"gadgetry_bfcontent_ads_image5\";s:0:\"\";s:27:\"gadgetry_bfcontent_ads_url5\";s:0:\"\";s:31:\"gadgetry_bfcontent_ads_adsense5\";s:0:\"\";s:29:\"gadgetry_bfcontent_ads_image6\";s:0:\"\";s:27:\"gadgetry_bfcontent_ads_url6\";s:0:\"\";s:31:\"gadgetry_bfcontent_ads_adsense6\";s:0:\"\";s:29:\"gadgetry_bfcontent_ads_image7\";s:0:\"\";s:27:\"gadgetry_bfcontent_ads_url7\";s:0:\"\";s:31:\"gadgetry_bfcontent_ads_adsense7\";s:0:\"\";s:19:\"gadgetry_hook_space\";s:5:\"false\";s:19:\"gadgetry_hook_image\";s:0:\"\";s:17:\"gadgetry_hook_url\";s:0:\"\";s:21:\"gadgetry_hook_adsense\";s:0:\"\";s:25:\"gadgetry_content_subtitle\";s:0:\"\";s:20:\"gadgetry_content_top\";s:0:\"\";s:23:\"gadgetry_content_bottom\";s:0:\"\";s:18:\"gadgetry_seo_title\";s:0:\"\";s:24:\"gadgetry_seo_description\";s:0:\"\";}"]}
The "array" you provided is incomplete. It has an unclosed string.
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
I've got a JSON string and I'm just trying to access the different properties of it and store them in Java variables. However, I keep getting an exception with the following code:
private JSONObject jObj;
private String jString;
//...
jString = result; //this is my JSON string passed from another activity
try {
jObj = new JSONObject(jString);
//int eventID = jObj.getInt("eventID");
} catch (JSONException e) {
Toast.makeText(searchResultsActivity.this, "Search results failed!", Toast.LENGTH_SHORT).show();
finish();
}
Yes I have the required imports. I've displayed jString on its own to confirm that it's valid JSON. I'm kind of lost because this seems to be the most basic thing I need to do. Thanks for any help guys.
EDIT - here is an example JSON string:
[{"eventID":"47","event_name":"test","event_address":"Test","event_duration":"3","event_date":"20110527","event_time":"1347","event_description":"Test","num_attending":"1"}]
This string is received through a PHP script where I do echo json_encode($array), where $array is the associative array creating this JSON response.
The exception I get is:
"org.json.JSONException: Value[//above JSON string//] of type org.json.JSONArray cannot be converted to JSONObject"
Eclipse did not tell you because you were trying to create a JSONObject from a JSONArray:
JSONArray jArr = new JSONArray (jString);
int eventID = jArr.getJSONObject(0).getInt("eventID");
To answer your last comment (why is this?):
From the (original documentation](http://www.json.org/java/index.html):
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get() and opt() methods for accessing the values by name, and put() methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.
A JSONArray is an ordered sequence of values. Its external form is a string wrapped in square brackets with commas between the values. The internal form is an object having get() and opt() methods for accessing the values by index, and put() methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.
Since you were parsing a string that started with square brackets instead of curly braces, you need to parse it as a JSONArray. In your case, it is an array of size 1.
You are trying to parse a JSONArray as a JSONObject
JSONArray jarray = new JSONArray(jString);
gl!