?
Solved

How to Serailize Object to Array Json C#

Posted on 2016-09-14
7
Medium Priority
?
75 Views
Last Modified: 2016-09-15
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

0
Comment
Question by:karthik80c
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 4

Accepted Solution

by:
Steven Kribbe earned 2000 total points
ID: 41799286
I think you should make Photos a List<product> and then pass it to the JsonConvert.SerializeObject.
1
 

Author Comment

by:karthik80c
ID: 41799293
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
0
 
LVL 4

Expert Comment

by:Steven Kribbe
ID: 41799319
How are you parsing the data in js?
0
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
LVL 4

Expert Comment

by:Steven Kribbe
ID: 41799323
Could be that there the data is already parsed by some ajax lib (jQuery) befor it is passed to JSON.parse.
0
 

Author Comment

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

Open in new window

0
 
LVL 4

Expert Comment

by:Steven Kribbe
ID: 41799342
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?
0
 

Author Comment

by:karthik80c
ID: 41799343
oh the response is object am getting
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question