将复杂值转换为普通字符串

本文关键字:字符串 转换 复杂 | 更新日期: 2023-09-27 18:01:44

如何转换这个值:

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);

将复杂值转换为普通字符串

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

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