我使用VB.NET“WCF Rest Service Application”项目模板构建了一个非常简单的Web服务(这是一个不错的选择吗?)。我工作得很好,除了有
的事实<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
...
</string>
添加到我的回答中。
我已将我的返回值声明为String:
<WebInvoke(UriTemplate:="member/login", Method:="POST",
responseformat:=WebMessageFormat.Json,
BodyStyle:=WebMessageBodyStyle.Bare)>
Public Function Create(data As IO.Stream) As String
Dim strData As String = New IO.StreamReader(data).ReadToEnd()
Dim UserAccessForm As LoginAccess = Me.getAnswer(strData)
Dim jsonAnswer As String
jsonAnswer = Newtonsoft.Json.JsonConvert.SerializeObject(UserAccessForm, Newtonsoft.Json.Formatting.None)
Return jsonAnswer
End Function
所以不要回答这个问题:
{"logged":false,"userID":"0","message":"Empty body"}
我明白了:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{"logged":false,"userID":"0","message":"Empty body"}
</string>
有什么方法可以避免我的字符串答案的这种不必要的序列化吗?
答案 0 :(得分:4)
我遇到了这个问题并通过将XmlSerializerFormat
属性应用于服务合同来解决它。
这是一个c#片段 - 希望它可以帮到你。
[ServiceContract(Namespace = "")]
[XmlSerializerFormat]
interface IHuggies
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "/CheckIfConsumerExists")]
bool CheckIfConsumerExists(string parameters);
}
答案 1 :(得分:2)
我认为这会有效,请注意Stream
作为返回参数。
<WebInvoke(UriTemplate:="member/login", Method:="POST",
responseformat:=WebMessageFormat.Json,
BodyStyle:=WebMessageBodyStyle.Bare)>
Public Function Create(data As IO.Stream) As Stream
Dim strData As String = New IO.StreamReader(data).ReadToEnd()
Dim UserAccessForm As LoginAccess = Me.getAnswer(strData)
Dim jsonAnswer As String
jsonAnswer = Newtonsoft.Json.JsonConvert.SerializeObject(UserAccessForm, Newtonsoft.Json.Formatting.None)
Return New MemoryStream(ASCIIEncoding.Default.GetBytes(jsonAnswer));
End Function
答案 2 :(得分:0)
这似乎可行(responseformat.Json是魔术字):
import 'package:actionhrm/pages/login.dart';
import 'package:flutter/material.dart';
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
import 'package:flutter/services.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
MyApp() {
//changeStatusColor(Color(0xFF28ace2));
}
changeStatusColor(Color color) async {
try {
await FlutterStatusbarcolor.setStatusBarColor(color);
} on PlatformException catch (e) {
print(e);
}
}
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData(
primaryColor: Color(0xFF28ace2),
brightness: Brightness.light,
accentColor: Colors.white,
cardColor: Colors.white,
),
home: LoginPage(),
);
}
}
答案 3 :(得分:0)
我对此进行了大量阅读,并且看起来XML是C#中的标准响应。我结束了:
$ cat test.sh
ls \
-l\
-h
$
答案 4 :(得分:-3)
public String cleanOUTPUT(String OUTPUT) {
OUTPUT = OUTPUT.replaceAll("<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">","");
OUTPUT = OUTPUT.replaceAll("</string>", "");
return OUTPUT;
}