The JsonDeserializationException encountered an error while attempting to deserialize the object

When receiving data from the BackEnd, it typically looks like this:

{name : String,
 author : String,
 updater : String,
 media : String,
}

Sometimes, the 'media' field can also be an array with a different structure, but unfortunately I am unable to modify the service. I would prefer to leave it unchanged.

 -media :[{type:String,
           path:String}
         {type:String
          path:String}]

If the 'type' in the media array is not a string as expected, I simply want to replace it with null.

For decoding the JSON string, I am utilizing Gson library.

Edit: This snippet shows part of my class structure:

private String id;
private String nom;
private String nom_en;
private String adresse;
private String adresse2;
private String cp;
private String ville;
private String pays;
private String coordonnees_x;
private String coordonnees_y;
private String contactName;
private String email;
private String telephone_1;
private String telephone_2;
private String fax;
private String website;
private String date_ajout;
private String ArticleRefId;
private String media;
private int type;
private List<AdresseCategorie> categories;
private String ArticleTitle;
private ArticleObject article;

Answer №1

If you're looking for options, one simple solution is to modify the media field to be of type Object.

By making this change, Gson will handle your JSON data differently depending on its structure. It will create a String if the JSON is formatted like the first scenario, or an array of Maps in the second scenario. These maps will have keys labeled as type and path. By using an instanceof check, you can then manipulate the media field according to your specific requirements.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Benefits of Using JSON in AJAX Requests

Can you help me determine the best way to extract data from a php page using ajax in the form of a json array? Here is an example code snippet: $.ajax({ type: "get", url: "assets/update_cart_user_fstore.php", data: up, cache: false, su ...

Arrange the JSON attributes in reverse order

Given the JSON data provided, how can I sort the "c" value in descending order? [{"id":"2","val":"2pm.com","c":"4"}, {"id":"2","val":"Adidas","c":"2"}, {"id":"2","val":"AJ Morgan","c":"2"}, {"id":"2","val":"Alfio Raldo","c":"3"}, {"id":"1","val":"ASOS"," ...

combine two JSON objects in Google Apps Script without using jQuery

Is there a way to combine two JSON objects in Google Spreadsheet script without using jQuery? {"records":[{"id":28100988,"work_text_reviews_count":13,"average_rating":"3.10"},{"id":10280687,"work_text_reviews_count":80,"average_rating":"3.87"}]} {"record ...

Exploring the Power of SQL in JSON Parsing (OPENJSON)

If you want to access your Google searches data, you can download it in the form of multiple JSON files. I am currently working on parsing them into columns named [TimeStamp] and [Query Text] using the SQL function OPENJSON. DECLARE @json as nvarchar(max) ...

Unraveling JSON through DOJO AJAX and REST techniques

I have been attempting to send a request to a REST server using DOJO AJAX, but unfortunately I am receiving a null object as the result in the console: When CLICKED = click clientX=34, clientY=13 JSON loaded from server: null Below is the code snippet I ...

Put the value of req.body inside quotation marks

Recently, I've been troubleshooting an issue with my Express REST API where the JSON body is being populated with unnecessary quotation marks. In my Express middleware setup, I have configured it as follows: app.use(bodyParse.urlencoded({ extended: ...

Utilizing Json data with Jquery for dynamically placing markers on Google Maps

Seeking assistance as I am currently facing a problem where I need to loop through JSON data and add it as markers on Google Maps, but unfortunately, it only returns null value. Is there a way to automatically connect this to JSON? My plan is to have a Gr ...

Find the most affordable rate in the JSON data without knowledge of the parent label

I am seeking to extract the best deal from JSON data obtained through an API call and decoded using json_decode. The structure is as follows: products productnumber -> price $product_array['products'][..changingnumber..]['value'] ...

Verify if the final (or initial) node containing a child with a specific value

Using XSLT 1.0, I have transformed the given XML into an array of JSON objects: <a id="x"> <active>Yes</active> </a> <a id="y"> <active>No</active> </a> <a id="z"> <active>Yes</act ...

Exploring the process of extracting a nested JSON value using Postman

I am currently facing an issue with parsing the json response from a post request, and then sending the parsed data to a put request. Here is the response body: { "createdBy": "student", "createdOn": "2019-06-18", "Id1": "0e8b9 ...

Mastering the extraction of key/value pairs from a JSON response in the OnComplete ajax

After executing a specific action, it is returning the following outcome: return Json(new { result = true, value = "hello" }, "text/html"); I'm wondering how can I retrieve the value in my onComplete function? onComplete: function (file, respo ...

Using AJAX to submit forms in Laravel

After creating a form to input data into the database, everything was functioning correctly until I implemented AJAX. The issue now is that even with AJAX, it still redirects me to another page and displays the text returned by the controller. Below is m ...

Ways to retrieve information from a POST request

I am looking for assistance on extracting data from a post request and transferring it to Google Sheets while also confirming with the client. Any guidance or support would be highly appreciated. Thank you. My project involves creating a web application f ...

Convert XML to JSON with Azure

Seeking assistance with transforming XML to JSON using Azure liquid mapping in order to extract the session ID. <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3. ...

Combining a JSON file and a CSV file into a pandas dataframe for analysis

I have a JSON file with geographic data that includes information on population counts for different areas represented by WK_CODE. { "type" : "FeatureCollection", "name" : "NBG_DATA.CBSWBI", "feature ...

Mapping JSON data from an array with multiple properties

Here is a JSON object that I have: obj = { "api": "1.0.0", "info": { "title": "Events", "version": "v1", "description": "Set of events" }, "topics": { "cust.created.v1": { "subscribe": { ...

I am looking to transform intricate json data into csv format with the help of either python or R programming

Converting values to rows in CSV and keys to columns in CSV format would be beneficial. { "_id": { "$uId”: “12345678” }, “comopany_productId”: “J00354”, “`company_product name`”: “BIKE 12345”, "search_resu ...

What is the process for invoking a Java function in Android from JavaScript when using Vue?

My project is developed using Vue.js. In order to view it on a mobile phone, I decided to create an Android app. To achieve this, I am using the WebView component. However, when I click on an element in the Vue.js app, JavaScript calls Java and some events ...

Is there a way to retrieve the initial item of a JSON array from an HTML document using Angular 2?

Within the src/assets/ directory, I have a json file called product.json with the following structure: [ { "images": "http://openclipart.org/image/300px/svg_to_png/26215/Anonymous_Leaf_Rake.png", "textBox": "empty", "comments": "empty" }, { "i ...

Retrieve the precise response parameter using urllib in Python

Is there a way to process the token information obtained from a web request using urllib in Python? from urllib import request from urllib.parse import urlencode response = request.urlopen(req, data=login_data) content = response.read() The response typic ...