0

I am trying to parse json response as follows:

["India",["India","India\u2013United States Civil Nuclear Agreement","Indian rupee"],["India, Test"],["https://en.wikipedia.org/wiki/India","https://en.wikipedia.org/wiki/India%E2%80%93United_States_Civil_Nuclear_Agreement"]]

Wrapper class:

public class fromJSON{
public String Text;    //India
public List<jsonTitles> jsonTitles;
public List<jsonDetails> jsonDetails;
public List<jsonURL> jsonURL;
public class jsonTitles{}
public class jsonDetails{}
public class jsonURL{}
public static fromJSON parse(String responseBody ){
System.debug('@@@@@@@' +JSON.deserialize(responseBody , fromJSON.class));
return (fromJSON) System.JSON.deserialize(responseBody , fromJSON.class);
}  
}

I am unbale to do as I land up with this error

Malformed JSON: Expected '{' at the beginning of object

I have concatenated json string to solve this issue I am getting following error after that:

System.JSONException: Unexpected character ('[line:1, column:3]

What I can understand from JSON is, it is in array with out key value pairs.

I have used deserialize methods of salesforce but unable to do this.Can anyone help me with snippet or the approach.??

But I am able to do with javascript: If JSON validator says it is valid response.Then with apex I should be able to do it??

How it is possible using JavaScript:

parseResult : function(res,helper){
        var retJSON = JSON.parse(res);
        var jsonTitles = retJSON[1] ; 
        var jsonDetails = retJSON[2];
        var jsonURL = retJSON[3];

        var domSearch = document.getElementById('resultPlaceHolder');
        while (domSearch.firstChild) {
            domSearch.removeChild(domSearch.firstChild);
        }

        for(var i=0 ; i<jsonTitles.length ; i++)
        {
           helper.createChildNodes(domSearch, jsonTitles[i],jsonDetails[i],jsonURL[i]); 
        }

    }
4

There are two resource that will help you out tremendously here:

Your payload is not in a valid format for several reasons. Here is the immediate JSONLint Result:

Results

Error: Parse error on line 3:
...Nuclear_Agreement", ]]
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got ']'

Your first problem you have is that you have a comma which is not followed by a subsequent list element. Lists are of the format:

[element1, element2, element3]

However, you have left a dangling comma:

[element1,]

Note the JSON spec on arrays:

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

array spec


Once you fix the above issue, your work is not done. If you want to deserialize this data structure into an Apex Class, it won't work. You also need to understand the object structure:

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

object spec

These keys will correspond to the properties in your class. So a properly formatted object would look like:

{
    "Text": "India",
    "jsonTitles": ["..."],
    "jsonDetails": ["..."],
    "jsonUrl": ["..."]
}

Change your jsonTitles, jsonDetails, and jsonUrl properties to the List<String> type. The custom classes you have defined cannot be serialized into anything useful.

If you plug this last payload into JSON2Apex, you'll get a simple class that should work for your needs:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

  public String Text;
  public List<String> jsonTitles;
  public List<String> jsonDetails;
  public List<String> jsonUrl;

  
  public static JSON2Apex parse(String json) {
      return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
  }
}

If you really want to stick with this poorly structured JSON format, you can use deserializeUntyped to get something workable:

List<Object> data = (List<Object>)JSON.deserializeUntyped(payload);
List<String> jsonTitles = (List<String>)data[1];
List<String> jsonDetails = (List<String>)data[1];
List<String> jsonURL = (List<String>)data[1];
| improve this answer | |
  • It is valid now you can check..removed comma from json response .. – Salesforce Learner Feb 3 '17 at 6:36
  • It was manual error from my end.But I am unable to parse the response – Salesforce Learner Feb 3 '17 at 6:38
  • See updated answer. You can't deserialize an array of different types into an object. You need to actually specify the keys. – Adrian Larson Feb 3 '17 at 6:47
  • Updated question as well – Salesforce Learner Feb 3 '17 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.