在C#中怎么解析JSON数据,并获取到其中的值?

Lifeihu 2010-06-26 02:58:10
在C#中怎么解析JSON数据,并获取到其中的值?
比如JSON数据为:{"phantom":true,"id":"ext-record-10","data":{"MID":1019,"Name":"aaccccc","Des":"cc","Disable":"启用","Remark":"cccc"}}。
我想得到结果为{"MID":1019,"Name":"aaccccc","Des":"cc","Disable":"启用","Remark":"cccc"}的字符串。
...全文
6390 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝜蝂 2013-03-14
  • 打赏
  • 举报
回复
引用 3 楼 findcaiyzh 的回复:
一时手痒帮你写了 C# code?1234567891011121314151617181920212223242526272829303132 public class InternalClass { public int MID; public string Name; ……
解决方案 可用
蝜蝂 2013-03-14
  • 打赏
  • 举报
回复
关注下 今天正好用到
宝_爸 2010-06-28
  • 打赏
  • 举报
回复
这个也结了吧:)

可以使用serialization得到,但是我觉得那最好还是用字符串函数来做性能好一点 serialization和desiralization比较慢。

const string json = @"{""phantom"":true,
""id"":""ext-record-10"",
""data"":
{""MID"":1019,
""Name"":""aaccccc"",
""Des"":""cc"",
""Disable"":""启用"",
""Remark"":""cccc""}}";

int posBegin = json.IndexOf(’{', 0);
posBegin = json.Indexof('{', posBegin);
int posEnd = json.IndexOf('}', posBegin);
string substr = json.SubString(posBegin, posEnd - posBegin +1);

代码没有经过测试,可能有小问题,仅供参考。
宝_爸 2010-06-26
  • 打赏
  • 举报
回复
新版,需要引用System.ServiceModel.Web.dll


using System.Runtime.Serialization.Json;
[Serializable]
public class InternalClass
{
public int MID;
public string Name;
public string Des;
public string Disable;
public string Remark;
}

[Serializable]
public class OuterClass
{
public bool phantom;
public string id;

public InternalClass data;
}

private void button2_Click(object sender, EventArgs e)
{
const string json = @"{""phantom"":true,
""id"":""ext-record-10"",
""data"":
{""MID"":1019,
""Name"":""aaccccc"",
""Des"":""cc"",
""Disable"":""启用"",
""Remark"":""cccc""}}";
DataContractJsonSerializer ser1 = new DataContractJsonSerializer(typeof(OuterClass));
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
OuterClass foo1 = ser1.ReadObject(ms) as OuterClass;
}

}
宝_爸 2010-06-26
  • 打赏
  • 举报
回复
一时手痒帮你写了


public class InternalClass
{
public int MID;
public string Name;
public string Des;
public string Disable;
public string Remark;
}

public class OuterClass
{
public bool phantom;
public string id;

public InternalClass data;
}

private void button2_Click(object sender, EventArgs e)
{
const string json = @"{""phantom"":true,
""id"":""ext-record-10"",
""data"":
{""MID"":1019,
""Name"":""aaccccc"",
""Des"":""cc"",
""Disable"":""启用"",
""Remark"":""cccc""}}";
JavaScriptSerializer ser = new JavaScriptSerializer();
OuterClass foo = ser.Deserialize<OuterClass>(json);


}
宝_爸 2010-06-26
  • 打赏
  • 举报
回复
下面的内容没有验证过,仅供参考

I don't know about JSON.NET, but it works fine with JavaScriptSerializer from System.Web.Extensions.dll (.NET 3.5 SP1):

using System.Collections.Generic;
using System.Web.Script.Serialization;
public class NameTypePair
{
public string OBJECT_NAME { get; set; }
public string OBJECT_TYPE { get; set; }
}
public enum PositionType { none, point }
public class Ref
{
public int id { get; set; }
}
public class SubObject
{
public NameTypePair attributes { get; set; }
public Position position { get; set; }
}
public class Position
{
public int x { get; set; }
public int y { get; set; }
}
public class Foo
{
public Foo() { objects = new List<SubObject>(); }
public string displayFieldName { get; set; }
public NameTypePair fieldAliases { get; set; }
public PositionType positionType { get; set; }
public Ref reference { get; set; }
public List<SubObject> objects { get; set; }
}
static class Program
{

const string json = @"{
""displayFieldName"" : ""OBJECT_NAME"",
""fieldAliases"" : {
""OBJECT_NAME"" : ""OBJECT_NAME"",
""OBJECT_TYPE"" : ""OBJECT_TYPE""
},
""positionType"" : ""point"",
""reference"" : {
""id"" : 1111
},
""objects"" : [
{
""attributes"" : {
""OBJECT_NAME"" : ""test name"",
""OBJECT_TYPE"" : ""test type""
},
""position"" :
{
""x"" : 5,
""y"" : 7
}
}
]
}";


static void Main()
{
JavaScriptSerializer ser = new JavaScriptSerializer();
Foo foo = ser.Deserialize<Foo>(json);
}


}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧