I have been trying to serialize some json data in Silverlight. I am using the following code
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(stacks.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, stacks);
StreamReader reader = new StreamReader(ms);
string json = reader.ReadToEnd();
to attempt the serialization. It does not work. It was the only example I could find that did not produce errors in Visual Studio. I am passing a list of custom coded objects (stacks). When I try to view the results I am getting a blank string. Anyone got some ideas on how to point me in the right direction?
The stream cursor is pointing to the end (after everything was written). Add the line "ms.Position = 0;" before creating the StreamReader.
Related
I want to get a video file from a byte array which I stored in an array.
I am creating a MemoryStream object and passing the bytearray there. as like this:
MemoryStream ms = new MemoryStream(arrByte);
After that i am creating an object of bitmap and passing the memorystream instance to this object. as like this:
Bitmap bm = new Bitmap(ms);
But due to some reason i am not able to convert it to bitmap. this is the error I am getting: as like this: error: 'Parameter is not valid.'
To overcome this error i tried to use: ms.Position = 0;, ms.Seek(0, SeekOrigin.Begin); but they dont work. Can we convert streaming bytearray of video to bitmap ??
We are using this article to overcome this error but it didnot work:
Parameter is not valid error when creating image from byte[] in c#
I need to know how to populate datatable from JSON string in vb.net code.
the JSON string
{"success":true,"message":"","result":[{"Paper1:null,"StateId":"57ee","School":"1A","Received":"2018-07-03T08:10:05.22","TimeS":"00","STAT":"98","ScoreCard":"76"},{"Paper1:null,"StateId":"52ef","School":"1A","Received":"2018-07-03T08:10:05.22","TimeS":"00","STAT":"88","ScoreCard":"57"}]}
I need to know how to populate above string to a datatable
The above string is in a WebResponse. So is there any way to read the webresponse (here I have used a streamreader, or any other good method) and populate to a datatable?
my code which I got above string.
Dim rqst As WebRequest = WebRequest.Create(uri_variable)
Dim res_p As WebResponse
rqst.Method = "GET"
rqst.Headers.Add("apisign:" & sign)
res_p = rqst.GetResponse()
Dim reader As New StreamReader(res_p.GetResponseStream())
Dim JSON_String as string = reader
P.S: EDIT: I included a screenshot for the user "CruleD"
https://i.stack.imgur.com/EYbP0.png
Heh, this again.
Imports System.Web.Script.Serialization ' for reading of JSON (+add the reference to System.Web.Extensions library)
Dim JSONC = New JavaScriptSerializer().DeserializeObject(JSON_String)
Inspect JSONC with breakpoint. You will see how it looks then you decide what you want to do with it.
Comparing files not working as intended
Similar answer I gave in another thread.
Edit:
JSONC("result")(0)("Quantity")
First you get the contents of "result", then you select which collection you want, in this case first (so 0) then you search for whatever key you want again eg "Quantity" like you did for result originally.
There are other ways to do the same thing, but this one should be pretty straight forward.
There are 3 types of deserialization, check them out if you want.
I want to save some data about tweets in a persistent json file. I'm using org.codehaus.jackson.JsonGenerator to write the tweets to a file, which works fine when they are inserted all at once. My code:
JsonGenerator g = new JsonFactory().createJsonGenerator(jsonFile, JsonEncoding.UTF8);
for(Tweet t : list){
g.writeStartObject();
g.writeStringField(ID_FIELD, t.getTweetID());
g.writeStringField(DATE_FIELD, dateFormat.format(t.getDate()));
if(t.isRetweet())
g.writeStringField(RETWEET_FIELD, t.getOriginalTweet());
g.writeArrayFieldStart(HASHTAG_FIELD);
for(String s : t.getHashtags())
g.writeString(s);
g.writeEndArray();
g.writeEndObject();
}
g.close();
Problem is, if I want to add another bunch of tweets to the same file, this function will overwrite the previous ones. I've been trying to keep the generator open instead of closing it every time, but it screwed up the process of writing in the first place. also, the API doesn't suggest any way of "continuing where I left off". No luck with other json streamers either so far. Suggestions?
Not sure if I understand correctly, but if the problem is that you can't append multiple JSON objects to your output - then just open a FileWriter and make sure that the target isn't auto-closed after each object write.
For instance:
FileWriter output = new FileWriter("/path/to/file");
ObjectWriter jsonWriter = new ObjectMapper()
.configure(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT, false)
.writer();
jsonWriter.writeValue(output, object1); output.write('\n');
jsonWriter.writeValue(output, object2); output.write('\n');
jsonWriter.writeValue(output, object3); output.write('\n');
I want to return a list of validation errors from my mvc app to the client side to I can take use of the jquery validation showErrors which takes an object
I can get a list of fields and the the erorr(s) that apply to them in any way that is best suited.
I have tried a few formats already, dictionary and none of these serialise to the correct structure as required by the validation library.
ie.
{"fieldname":"some error for fieldname", "fieldname2": "some error for fieldname2"}
All of my examples seem to serialise into something along the lines of
{"Key": "fieldname", "Value" : "some error for fieldname"}
What is the best way to return my data and how can I get it serialised in the correct way that I need?
I suggest you use Json.NET (it is default JSON serializer for ASP.NET MVC 4 as well) and its JsonWriter:
StringWriter errorsStringWriter = new StringWriter();
JsonWriter errorsJsonWriter = new JsonWriter(jsonStringWriter);
errorsJsonWriter.WriteStartObject();
errorsJsonWriter.WritePropertyName("fieldname");
errorsJsonWriter.WriteValue("some error for fieldname");
errorsJsonWriter.WritePropertyName("fieldname2");
errorsJsonWriter.WriteValue("some error for fieldname2");
...
errorsJsonWriter.WriteEndObject();
errorsJsonWriter.Flush();
You can return JSON generated this way with ContentResult:
return Content(errorsStringWriter.GetStringBuilder().ToString(), "application/json");
UPDATE
Json.NET also supports dynamic JSON through JObject. In that case your code can look like this:
var jsonObject = new JObject();
jsonObject.Add("fieldname", "some error for fieldname");
jsonObject.Add("fieldname2", "some error for fieldname2");
...
Creating ContentResult in this case can look like this:
return Content(jsonObject.ToString(Newtonsoft.Json.Formatting.None), "application/json");
using RestSharp is there a way to get the raw json string after it has been deserialized into an object? I need that for debugging purposes.
I'd like to see both the deserialized object and the originally received json string of that object. It's part of a much bigger json string, an item in an array and I only need that specific item json code that's got deserialized into the object.
To help you out this should work, this is a direct example of some of my work, so your's might be a bit different.
private void restClient()
{
string url = "http://apiurl.co.uk/json";
var restClient = new RestClient(url);
var request = new RestRequest(Method.GET);
request.AddParameter("apikey", "xxxxxxxxxx");
restClient.ExecuteAsync<Entry>(request, response =>
{
lstboxtop.Items.Add(response.Content);
});
}
The line lstboxtop is a listbox and using the response.content will literally print the whole api onto your app, if you call it first that would be what you are looking for