Avatar of karthik80c
karthik80c
Flag for United States of America asked on

How to Serailize Object to Array Json C#

Hi Experts ,

Am trying to serailize a dynamic object to array . but its not working correctly and here is my code
dynamic product = new JObject();
 foreach (PropertyImage tbl in LST)
                {

                    product.FullImage = SiteUrl + "/" + tbl.Photo;
                    product.imageThumb = SiteUrl + "/" + tbl.Photo;
                    product.description = tbl.Tag;
                    product.Address = Address;
                    product.imageid = tbl.OrderNumber;
                    product.TextColor = "White";

                    if (Photos != "")
                    {
                        Photos += "," + product.ToString();
                    }
                    else
                    {
                        
                        Photos += product.ToString();
                    }
                }
                //Json += Photos;
                // Json += "]";

                var json = JsonConvert.SerializeObject(Photos, Formatting.Indented); ;

Open in new window


but am getting json  output as
"{\r\n  \"FullImage\": \"http://localhost:57226/img/image1.JPG\",\r\n  \"imageThumb\": \"http://localhost
:57226/img/image1.JPG\",\r\n  \"description\": \"Kitchen\",\r\n  \"Address\": \"Test\",\r\n  \"imageid
\": 1,\r\n  \"TextColor\": \"White\"\r\n},{\r\n  \"FullImage\": \"http://localhost:57226/img/image1.JPG
\",\r\n  \"imageThumb\": \"http://localhost:57226/img/image1.JPG\",\r\n  \"description\": \"Bedroom\"
,\r\n  \"Address\": \"Test\",\r\n  \"imageid\": 1,\r\n  \"TextColor\": \"White\"\r\n}"

Open in new window

JSONC#ASP.NET.NET Programming

Avatar of undefined
Last Comment
karthik80c

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Steven Kribbe

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
karthik80c

ASKER
Thanks Steven Kribbe that works

[
  {
    "FullImage": "http://localhost:57226/img/image1.JPG",
    "imageThumb": "http://localhost:57226/img/image1.JPG",
    "description": "http://localhost:57226/img/image1.JPG",
    "Address": "Test",
    "imageid": "1",
    "TextColor": "White"
  },
  {
    "FullImage": "http://localhost:57226/img/image1.JPG",
    "imageThumb": "http://localhost:57226/img/image1.JPG",
    "description": "http://localhost:57226/img/image1.JPG",
    "Address": "Test",
    "imageid": "1",
    "TextColor": "White"
  }
]


 . but now js throws error as SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data . Any idea on this
Steven Kribbe

How are you parsing the data in js?
Steven Kribbe

Could be that there the data is already parsed by some ajax lib (jQuery) befor it is passed to JSON.parse.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
karthik80c

ASKER
am using  
 $.ajax({
       
        url: "Imagehandler.ashx?propertyid=" + propertyid
        
    }).done(function (rsp) {
        debugger;
   var propertyData = JSON.parse(rsp);

Open in new window

Steven Kribbe

The $.ajax may already interpret the http response as JSOn and deliver the rsp as a parsed JSON object. Can you check if the rsp is an object or a string?
karthik80c

ASKER
oh the response is object am getting
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.