Get string from JSON array? - java

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.

Related

How to put data to parsed JSONObject

i have some parse code and to parsed JSONObject i need to add one more JSONObject, but getting error Unexpected token LEFT BRACE({), because my code creating multiply JSONObjects in file, not at parsed JSONObjec. Here is a code, that creating object
aJson = (JSONObject) parser.parse(reader);
JSONObject json = new JSONObject();
JSONArray blockData = new JSONArray();
for(Block b : blocks){
json.put("player-name", p.getName());
json.put("uuid", p.getUniqueId().toString());
json.put("nearestPlayers", new JSONArray());
blockData.add(b.getLocation().getWorld().getName());
blockData.add(b.getLocation().getWorld().getEnvironment());
blockData.add(b.getLocation().getX());
blockData.add(b.getLocation().getY());
blockData.add(b.getLocation().getZ());
}
aJson.put(blockData, json);
Here is JSON
{"[\"world\",NORMAL,-23.0,67.0,75.0]":{"player-name":"MisterFunny01","nearestPlayers":[],"uuid":"206d32da-bf72-3cfd-9a26-e374dd76da31"}} //here is that part// {"[\"world\",NORMAL,-23.0,67.0,75.0]":{"player-name":"MisterFunny01","nearestPlayers":[],"uuid":"206d32da-bf72-3cfd-9a26-e374dd76da31"},"[\"world\",NORMAL,-23.0,67.0,75.0]":{"player-name":"MisterFunny01","nearestPlayers":[],"uuid":"206d32da-bf72-3cfd-9a26-e374dd76da31"}}
In JSON array values must be of type string, number, object, array, boolean or null. Arrays hold values of the same type and not different types.
Looking at your code the array is an array of objects. So you would have to create an object and add the values before adding to the array.
Don't directly add values to the array but create an object and then add to the array.
Your code is wrong. To put an object into JSONObject please read this document
In your case, you need to convert blockData to String to put in the JSONObject.
It's like this: aJson.put(blockData as String, json);
Hope it can be helpful to you.

How to extract data from Json array?

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.

passing JSON array from JavaScript to java and convert JSON array to java array

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.

parse json data already in an array?

I have the following data:
[{"class":"test","description":"o hai","example":"a","banana":"b"}]
As this JSON data is already in an array, I'm having troubles to parse this with JSON simple:
File file = new File( "/Users/FLX/test.json");
String s = FileUtils.readFileToString(file);
Object obj = parser.parse(s);
JSONArray array = (JSONArray) obj;
log.warn("WAAAAT"+array.get(1));
This doesn't work because "1" (description) is in array 0, which causes an out of bounds exception, how can I properly do this?
[] denotes an array, while {} denotes an object, so you have an array of objects.
The way your JSON is formatted, you have an array which contains a single object. That single object has properties named "class", "description", "example", and "banana" with values "test", "o hai", "a", and "b" respectively.
JSONArray is 0 based so array.get(1) would be out of bounds. In order to get description you would do something like array.getJSONObject(0).get("description")

Android - parsing JSON strings in Java

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!

Categories

Resources