开发者

LINQ to JSON: how to get the right format?

开发者 https://www.devze.com 2023-04-05 06:49 出处:网络
im trying to convert my result from a linq query to json using json.net here is what i did: JObject o = JObject.FromObject(new

im trying to convert my result from a linq query to json using json.net

here is what i did:

JObject o = JObject.FromObject(new
{
    UserID = from u in model.USER
             select new
             {
                 UserID = u.UserID
             }
});

here is what i get:

JSON
  id=1
  result={"UserID":[{"UserID":121}开发者_开发知识库,"UserID":121},{"UserID":122},{"UserID":123},{"UserID":124}]}

here is what i need:

JSON
  id=1
  result={[{UserID:'121'},{UserID:'121'},{UserID:'122'},{UserID:'123'},{UserID:'124'}]}  

how do i get this done? thank you


Try this:

 JArray a = JArray.FromObject(
                from u in model.USER
                select new
                {
                    UserID = u.UserID
                }
    );
0

精彩评论

暂无评论...
验证码 换一张
取 消