You need to sign in to do that
Don't have an account?
System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
Hi,
I've created below class and want to display records in Visual source page.
public class JSONTest {
Public string strName{get;set;}
Public string strPrint {get;set;}
Public List<items> itemList {get;set;}
String jsonStr = '{"feed":{"row_count":1,"items":[{"msg":"<a class="keyword" href=http://abc.html>XYZ</a>Clicked<a class="keyword" href=http://abc.html>http://abc.html</a>","interest":"Other","whendt":"3/26/2012 6:22:49 PM"}]}}';
public void PrintJSONRecords(){
itemList = (List<items>)JSON.deserialize(jsonStr ,List<items>.class);
}
public void submit()
{
PrintJSONRecords();
}
class feed {
public Integer row_count;
public items[] itemss;
public feed(Integer a) {
this.row_count = a;
this.itemss = new List<items>();
}
}
public class items{
public String msg;
public String interest;
public DateTime whendt;
public items( String msg, String interest, DateTime whendt) {
this.msg = msg;
this.interest= interest;
this.whendt = whendt;
}
}
}
When I execute program it gives me below error;
System.JSONException: Malformed JSON: Expected '[' at the beginning of List/Set
What I'm missing?
Thanks!!
try
[{"feed":{"row_count":1,"items":[{"msg":"<a class='keyword' href=http://abc.html>XYZ</a>Clicked<a class='keyword' href=http://abc.html>http://abc.html</a>","interest":"Other","whendt":"3/26/2012 6:22:49 PM"}]}}]
The problem is that you're trying to deserialize a JSON object (not an array) into a list
A solution to your problem is:
Note that double quotes in JSON are double-escaped: \\". I believe it might be a bug in the System.JSON class.
Also, you need to format the date to make it serializable.
-Shamil
Thanks guys!!
Shamil, 2nd String variable worked well.
I've a doubt, Unless and untill I do not get perticular JSON values in an Array or List, how should I format them (e.g. Date)?
Also, how do I bind them to VF pages?
Thanks again!!
Can you control the dateTime format for the JSON you're receiving?
The format that JSON.deserialize() understands is ISO 8601: http://en.wikipedia.org/wiki/ISO_8601
In terms of VF binding - that's an unrelated problem, it all depends on how exactly you want to present these values on your VF page.
-Shamil
Thanks Shamil !
I'm receiving datetime thru webservice and format is '3/26/2012 6:22:49 PM' (I can not edit web service to change datetime format) , so how do I convert it in supported format?
Also, I've written code below;
public class JSONTest {
List<items> feeds;
String jsonStr2 = '[{"msg":"<a class="keyword" href=http://abc.html>XYZ</a>Clicked<a class="keyword" href=http://abc.html>http://abc.html</a>","interest":"Other","whendt":"2012-02-15T18:03:32-08:00"}]';
public List<items> getFeeds() {
return feeds;
}
public PageReference doSearch() {
feeds = (List<items>)getData();
return null;
}
public List<items> getData() {
List<items> data = new List<items>();
String JSONContent, strMessage, strInterest, strWhenDate;
HttpRequest req = new HttpRequest();
Http http = new Http();
//set the Method
req.setMethod('GET');
// generate the url for the request
String url = 'http://demowebservice.ashx';
//add the endpoint to the request
req.setEndpoint(url);
// create the response object
HttpResponse resp = http.Send(req);
// the Feed service is returning a line feed so parse it out
JSONContent = resp.getBody().replace('\n', '');
JSONParser parser = JSON.createParser(JSONContent);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)) {
if(parser.getText() == 'msg'){
parser.nextToken();
strMessage = parser.getText();
}
else if(parser.getText() == 'interest'){
parser.nextToken();
strInterest = parser.getText();
}
if(parser.getText() == 'when'){
parser.nextToken();
strWhenDate = parser.getText();
}
//store message, interest and date in a list
data.add(new items(strMessage, strInterest ,strWhenDate));
}
}
return data;
}
class FeedWraper {
public Integer row_count;
public items[] items;
public FeedWraper (Integer a) {
this.row_count = a;
this.items = new List<items>();
}
}
public class items{
public String msg { get; set; }
public String interest { get; set; }
public String whendt { get; set; }
public items( String msg, String interest, String whendt) {
this.msg = msg;
this.interest= interest;
this.whendt = whendt;
}
}
}
Values are getting displayed in table but showing multiple records instead of single records.
I'm doing something wrong in add items to list code. please, do let me know.
I'm displaying it in VF page using;
<apex:pageBlockTable value="{!feeds}" var="l" rendered="{!NOT(ISNULL(feeds))}">
<apex:column ><apex:outputLabel escape="false" value="{!l.msg}"></apex:outputLabel></apex:column>
<apex:column value="{!l.interest}"/>
<apex:column value="{!l.whendt}"/>
</apex:pageBlockTable>
Thanks again for follow up!!
I have a similar problem getting -
Exception :Malformed JSON: Expected '[' at the beginning of List/Set:null:System.JSONException
Appreciate any isnsights. Thx!
public class NormalizedAddresses {
public Integer confidence;
public Integer comprehension;
public Integer nameCount;
public String nameForm;
public String accoutntType;
public List<String> normalizedLines;
public List<Names> names;
public String context;
public String houseNumber;
public String streetName;
public String city;
public String state;
public String postalCode;
}
public class Names {
public String rootFirstName;
public String firstName;
public String middleName;
public String lastName;
public String businessName;
public String nameForm;
}
JSONString = '{"normalizedAddresses":[{"confidence":0,"comprehension":0,"nameCount":2,"nameForm":"1","accoutntType":"NULL NULL NULL NULL","normalizedLines":["ALLEN GORDON AND DEBBIE","NULL","NULL NULL NULL"],"names":[{"rootFirstName":"GORDON","firstName":"GORDON","middleName":"","lastName":"ALLEN","businessName":"","nameForm":"1"},{"rootFirstName":"DEBORAH","firstName":"DEBBIE","middleName":"","lastName":"ALLEN","businessName":"","nameForm":"1"}],"context":"a1mQ00000039BHlIAM","houseNumber":"","streetName":"","city":"","state":"","postalCode":""}]}';
List<NormalizedAddresses> deserializedString = (List<NormalizedAddresses>)JSON.deserialize(JSONString, List<NormalizedAddresses>.class);
following worked for me :
Http httpreq = new Http();
HttpResponse res = httpreq.send(req);
String reqresponse = res.getBody().replace('{"size":124,"totalSize":124,"done":true,"queryLocator":null,"entityTypeName":"ApexClass","records":','');
apxCls = (List<ApexClass>)JSON.deserialize(reqresponse, List<ApexClass>.Class);
return apxCls;