简体   繁体   中英

How to convert String array containing nested array to JSON in c#

How to convert sub-array to json. I have tried

 JArray _extra = JArray.Parse(extra.OrderOfferObject);
 if (_extra.Count() > 0)
    return Ok(new
    {
       Data = _extra, // ---
    }

So it did convert the string to JArray but skip the inner array. Here's the response:

Data: [{ Category: "Chicken Rice",
         Ingredients: "[{ExtraQuantity=1, ExtraPrice=11.99, ExtraTitle=Regular}, 
        {ExtraQuantity=1, ExtraPrice=0.0, ExtraTitle=Stuffed Cheese}, 
        {ExtraQuantity=1, ExtraPrice=0.0, ExtraTitle=Sauce BBQ}}]"}]

How do i convert the inner one, thanks.

Update

var extra = (from orderOffer in extraEntities.ORDER_OFFER
      where orderOffer.OrderOfferId == orderOfferId

      select new
      {
         orderOffer.OrderOfferObject
      }).FirstOrDefault();

Where OrderOfferObject is an array of string type having nested array.I tried JArray.parse to convert to JsonArray. Alas! it did not convert the nested array, as you can see in response.

Seems like your nested string is in a wrong format.

Currently your string object is this:

{ExtraQuantity=1, ExtraPrice=11.99, ExtraTitle=Regular}

It should be like this:

{ExtraQuantity: 1, ExtraPrice: 11.99, ExtraTitle: Regular}

Once your object is fine. JArray. Parse () only will do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM