0

i struggle to find a tool to convert this JSON:

{  
    "event":"subscribe",
    "feed":"ticker_lite",
    "product_ids":[  
        "FI_XBTUSD_180921",
        "FV_XRPXBT_180615"
    ]
}

into my c# code in the following format:{\"....\":\"....\"}

example in c#:

  var subMsg = "{\"type\": \"subscribe\",\"product_ids\": [\"BTC-EUR\"],\"channels\": [\"matches\"]}";

sorry for this basic questions,i am a newbe in json. Maybe JSON.net can help me but I did not find the right tool. thanks for any help

itriad
  • 49
  • 6
  • What didn't work about JSON.net? – itsme86 May 23 '18 at 21:40
  • nothing didn't work with JSON.net – itriad May 23 '18 at 21:41
  • JSON.net is used successfully by thousands of people. Maybe you just need to read the documentation? – itsme86 May 23 '18 at 21:42
  • i didn't find wich tool i must use to convert to -->:{\"....\":\"....\"} – itriad May 23 '18 at 21:43
  • 1
    I don't understand why you want to covert it to that. Seems like an XY problem to me. You usually need to serialize an object into a JSON string, or deserialize a JSON string into an object. I've never seen the requirement to change a JSON string into a differently formatted JSON string. What are you actually trying to achieve? – itsme86 May 23 '18 at 21:45
  • why you want to covert it to that,--->because i need this format in my project you dont need to understand everythings – itriad May 23 '18 at 21:51
  • i need this format--->"{\"type\": \"subscribe\",\"product_ids\": [\"BTC-EUR\"],\"channels\": [\"matches\"]}" – itriad May 23 '18 at 21:51
  • No, I don't need to know everything. And I also don't need to help. I don't ask clarifying questions for no reason. – itsme86 May 23 '18 at 21:52
  • thanks anyway,i try to stay very basic sorry – itriad May 23 '18 at 21:54
  • Are you sending this json over to javascript? – torsan May 23 '18 at 22:02
  • yes,i try but is not a success for now – itriad May 23 '18 at 22:07
  • Have you looked here https://stackoverflow.com/questions/1242118/how-to-escape-json-string – torsan May 23 '18 at 22:10

2 Answers2

0

Looks like you just need to deserialize the JSON string into a C# object. Deserializing means convert the JSON structure to a C# object you can work with. C# has builtin functions to do this.

On the other hand, your c# code looks more like javascript to me.

Check out the following read:

https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-serialize-and-deserialize-json-data

Varlain Systems
  • 306
  • 1
  • 7
  • yes its my issue i have a JSON data,i wanna to convert to javascript style because i want to send this to a websocket server in this way : {\"type\": \"subscribe\",\"product_ids\": [\"BTC-EUR\"],\"channels\": [\"matches\"]} – itriad May 23 '18 at 22:01
0

It really looks like you're need to turn this JSON object to string, and escape the quotes. The code below is all Javascript in HTML, and it outputs the subMsg in the format that you require:

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <title>test</title>

    <script>


        var myJsonObj = {
            "event": "subscribe",
            "feed": "ticker_lite",
            "product_ids": [
                "FI_XBTUSD_180921",
                "FV_XRPXBT_180615"
            ]
        }


        function escapeJSON(jsonObj) {

            var strJsonObj = JSON.stringify(jsonObj);
            alert("Was: " + strJsonObj);

            strJsonObj = strJsonObj.replace(/\"/g, "\\\"");
            alert("Now: " + strJsonObj);
            return strJsonObj;
        }

        var subMsg = escapeJSON(myJsonObj);
        alert("subMsg is: " + subMsg);

    </script>
</head>
<body>

</body>
</html>
Varlain Systems
  • 306
  • 1
  • 7