【问题标题】:Converting complex value to a normal string将复数值转换为普通字符串
【发布时间】:2016-08-10 08:33:14
【问题描述】:

我怎样才能转换这个值:

string str = ""m1":"DAF","m2":"LF55.220 E16","m3":"Africa Commercial Vehicles","m4":"sdgdf","m5":"gdfg"";

到一个普通的字符串?我可以在前面和后面添加更多引用,但我得到了同样的错误。

我收到如下错误:

; expected
} expected

看起来很简单,但我不知道如何将其转换为字符串。

编辑:

这是我从应用程序中获得的另一个示例。该值是一个对象。

Object {1: "DAF", 2: "FA LF55.220 E15", 3: "Barloworld Isuzu Trucks City Deep", 4: "dsfsdfsdfdsfsddsfdsfdsf", 5: "fsdfdsfdsdsfdsfdsfdsfds", 6: "dsfdsfdsfdsfdfdssddsf", 7: "dsfsddsfdfdssdfsdfdsf", 8: "sfsdfdsfdsdfdsfsdfsfd", 9: "sdfdsfsdsdsdfdsfdsfds", 10: "Hankook", 11: "11", 12: Object, 13: "Yes", 14: "Repair", 15: "Yes", 16: "Yes", 17: "Yes", 18: "Yes", 19: "Yes", 20: "Yes", 21: "Yes", 22: "Yes", 23: "Yes", 24: "Yes", 25: "Yes", 26: "Yes", 27: "Yes", 28: "Yes", 29: "Yes", 30: "Yes", 31: "Yes", 32: "Yes", 33: "Yes", 34: "Yes", 35: "Yes", 36: "Yes", 37: "Yes", 38: "Yes", 39: "Yes", 40: "Yes", 41: "Yes", 42: "Yes", 43: "Yes", 44: "Yes", 45: "df sfsdfsdfdfsdf d fsf sdf sdfsd fsdf sdf", 46: "1"}

这是控制台日志中的值。

编辑2:这是方法。它来自另一个编码器,并且是用 javascript 编码的。

$scope.postData = {
    m1 : M1.Name,
    m2 : M2.Name, 
    m3 : M3.Name, 
    m4 : M4, 
    m5 : M5, 
    ...   
};


var post = new Post($scope.postData);
  post.$save(function(postObject) {
    alert(JSON.stringify(postObject));
  });

.factory('Post', function($resource) {
    console.log('factory');
 return $resource('http://localhost:53101/Service.svc/BOB');//addchecklist 
});

编辑 3:这是 c# 方法和 WCF OperationContract

   public string BOB(Stream streamdata)
    {
        StreamReader reader = new StreamReader(streamdata);
        string res = reader.ReadToEnd();

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < 100; i++)
        {
            sb.AppendLine(res);
        }
        sb.Replace(":", "|").Replace(",", "|").Replace("{", "").Replace("}", "");

        string[] arData = sb.ToString().Split('|');
        string a = arData[0];
        string b = arData[1];
        string c = arData[2];
        string d = arData[3];
        string e = arData[4];
        string f = arData[5];
        string g = arData[6];
        string h = arData[7];
        reader.Close();
        reader.Dispose();
        return "Received: " + res;
    }

 [OperationContract]
        [WebInvoke(UriTemplate = "/BOB", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        string BOB(Stream streamdata);

【问题讨论】:

  • 您能提供您从移动应用程序收到的原始数据吗?
  • 请显示您要转换为字符串的变量/值。我猜你所需要的只是将它们与+ 连接起来
  • @CareTaker22 - 很酷 :) 现在我们可以看到上下文了。那么现在你的问题是什么?你在 C# 中得到了这个 javascript 对象,你想做什么?
  • 我想将整个对象转换为 c# 中的字符串,但我的对象中的值很复杂,无法将其转换为字符串。我需要将其转换为字符串,以便可以拆分字符串中的所有值并删除不需要的逗号和引号 :)
  • @CareTaker22- 你能展示一下收到这条消息的 C# 代码吗?

标签: javascript c# json string


【解决方案1】:

你应该做的是用\"转义"

string str = "\"m1\":\"DAF\",\"m2\":\"LF55.220 E16\",\"m3\":\"Africa Commercial Vehicles\",\"m4\":\"sdgdf\",\"m5\":\"gdfg\"";

【讨论】:

  • 问题是我直接从移动应用程序获取值,我需要将该值保存到字符串中。我无法使用上述方法逃脱,因为我首先无法将值转换为字符串:(
  • @CareTaker22 - 是的,但你能展示给你带来的代码吗?它看起来像一个 JSON。请查看如何更清楚您的需求
  • 很抱歉把问题发得这么差,但我急于完成一些其他工作。代码在 javascript 中完成并发送到 wcf 服务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 2014-08-06
相关资源
最近更新 更多