1

Creating a .Net 3.5 assembly that is making a call to PHP server. Return value a JSON object. The JSON object has a Base64 encoded WAV file (see below). Trying to use Newtonsoft.Json to get the WAV converted from "value". Spent 8 hours trying...

here is the code I have tried

int count = 0;
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
count = resStream.Read(buf, 0, buf.Length);
string json = Encoding.ASCII.GetString(buf, 0, count);

JObject jo = JObject.Parse(json);
//JObject jo = (JObject)JsonConvert.DeserializeObject(json);

This is where I get stuck. Error reads:

"Unterminated string. Expected delimiter: \". Path 'result.value', line 1, position 3449."

Almost like the string is too long...

Have not even gotten to the Base64 convert code I sourced off the internet that needs to be tweaked

char[] base64CharArray;
inFile = new System.IO.StreamReader(inputFileName,System.Text.Encoding.ASCII);
base64CharArray = new char[inFile.BaseStream.Length];
inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length);
base64String = new string(base64CharArray);

Solution is to go live first thing Monday...

Coder
  • 43
  • 1
  • 7
  • First, are you having trouble getting the base64 string out of the JSON, or having trouble converting the base64 string to a wav file? Second, what have you tried so far? – Jason Watkins Mar 24 '13 at 00:41
  • Jason tried getting the base64 string out of the JSON. Have not even got to the base64 to wav convert. I used this to try and help me: http://james.newtonking.com/projects/json/help/html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm http://stackoverflow.com/questions/10585500/deserialize-dicionary-with-json-net http://stackoverflow.com/questions/3391209/deserialize-json-base64-binary-in-net-using-datacontractjsonserializer http://stackoverflow.com/questions/11126242/using-jsonconvert-deserializeobject-to-deserialize-json-to-a-c-sharp-poco-class – Coder Mar 24 '13 at 00:55

1 Answers1

2

this should work fine. Probably not the most efficient solution though. don't forget to close your streams though, since they implement IDisposable.
I'm using WebClient here, but it doesn't really makes a difference. I think that HttpWebRequest is redundant in this case.

            WebClient client = new WebClient();
            Stream stream = client.OpenRead("http://waps.repli-con.com/services/ivrservices/getUserNameRecording/10000");
            StreamReader reader = new StreamReader(stream);

            JToken token = JObject.Parse(reader.ReadToEnd().ToString());

            string base64string = token.SelectToken("result").SelectToken("value").ToString();
            Byte[] b = Convert.FromBase64String(base64string);
            System.IO.File.WriteAllBytes(@"C:\Users\user\Desktop\test.wav", b);   
            stream.Close();
RAS
  • 3,375
  • 15
  • 24