i've got an IList<Foo>
and I'm trying to serialize it as Json
without the field names included in the result. As such, i'm trying to create an anonymous object, which i pass to the Json serialization method.
我有一个IList
Foo is defined as (pseudo code):-
Foo定义为(伪代码): -
public class Foo
{
public int X;
public int Y;
}
When i return this as Json ...
当我以Json的身份归还时......
return Json(foos);
the result is like
结果是这样的
... [{"X":1,"Y":2},{"X":3,"Y":4}...]
I don't want the X and Y to be there. So i'm after..
我不希望X和Y在那里。所以我在追求..
... [{1,2},{3,4}...]
So i was trying to do the following (which doesn't work)
所以我试图做以下(这不起作用)
(from p in foos
select new p.X + "," + p.Y).ToArray()
or
(from p in foos
select new string(p.X+ "," + p.Y)).ToArray()
but to no avail (doesn't compile).
但无济于事(不编译)。
Can anyone help, please?
有人可以帮忙吗?
4 个解决方案
#1
(from p in foos
select String.Format("{{{0}, {1}}}", p.X, p.Y)).ToArray()
#2
foos.Select(p=>p.X + "," + p.Y)
Or if you perfer Linq Syntax:
或者如果你更喜欢Linq语法:
(from p in foos
select p.X + "," + p.Y).ToArray()
#3
JSON is a member serializer, and uses the names. If you don't want names, don't use JSON. Perhaps just StringBuilder
, then?
JSON是成员序列化程序,并使用名称。如果您不想要名称,请不要使用JSON。或许只是StringBuilder,那么?
StringBuilder sb = new StringBuilder("[");
foreach(var foo in items) {
sb.Append('{').Append(foo.X).Append(',').Append(foo.Y).Append("},");
}
sb[sb.Length - 1] = ']';
string s = sb.ToString();
#4
Why not just create the JSON string yourself, so you have complete control over how it is set up rather than using the Serialization method.
为什么不自己创建JSON字符串,这样您就可以完全控制它的设置方式,而不是使用Serialization方法。
#1
(from p in foos
select String.Format("{{{0}, {1}}}", p.X, p.Y)).ToArray()
#2
foos.Select(p=>p.X + "," + p.Y)
Or if you perfer Linq Syntax:
或者如果你更喜欢Linq语法:
(from p in foos
select p.X + "," + p.Y).ToArray()
#3
JSON is a member serializer, and uses the names. If you don't want names, don't use JSON. Perhaps just StringBuilder
, then?
JSON是成员序列化程序,并使用名称。如果您不想要名称,请不要使用JSON。或许只是StringBuilder,那么?
StringBuilder sb = new StringBuilder("[");
foreach(var foo in items) {
sb.Append('{').Append(foo.X).Append(',').Append(foo.Y).Append("},");
}
sb[sb.Length - 1] = ']';
string s = sb.ToString();
#4
Why not just create the JSON string yourself, so you have complete control over how it is set up rather than using the Serialization method.
为什么不自己创建JSON字符串,这样您就可以完全控制它的设置方式,而不是使用Serialization方法。