Browsing 7239 questions and answers with Jon Skeet

Convert json object to model if that object is dynamic

I need the solution to convert json object to model if that object is number and i want to access the fields of that object. {"threaded_extended": { "857334186": [ { "id":...
Jon Skeet
people
quotationmark

Your question is somewhat unclear, but I suspect I see the pattern - the value of threaded_extended is effectively a Dictionary<int, List<Post>> or similar. So that's how you should represent it: public class Root { ... more 5/12/2017 6:03:43 AM

people

Ordering a string list by an index list in c#

I have two list, one contains string data and the second one contains some index numbers. List<int> myIndexList = new List<int>() {4, 3, 1, 2, 5,...
Jon Skeet
people
quotationmark

I think you just want: var orderedByIndexList = myIndexList.Select(index => myValuesList[index]); If you need it as a List<T>, put .ToList() at the end. Note that this doesn't modify myValuesList - it just creates a new... more 5/11/2017 2:48:07 PM

people

Convert string to json doesn't work java

I have a problem with convert string to json. Namely, my json string is: {"serverId":2,"deviceId":736,"analysisDate":"2017-05-11T07:20:27.713Z","eventType":"Logs","eventAttributes":[{"name":"level","value":"INFO"},{"name":"type","value":"Video Blocked On"},{"name":"cameraId","value":"722"},{"name":"userId","value":"1"}]} My code: try { JSONObject object = new JSONObject(jsonString); JSONArray array = object.getJSONArray("eventAttributes"); System.out.println("ARRAY: " + array); for (int i = 0; i < array.length(); i++) { JSONObject obj = new JSONObject(array.getJSONObject(i)); System.out.println("OBJ: " + obj); } } catch (JSONException ex) { Exceptions.printStackTrace(ex); } System.out.println array is: [{"name":"level","value":"INFO"},{"name":"type","value":"Video Blocked On"},{"name":"cameraId","value":"722"},{"name":"userId","value":"1"}] but if I print obj is "{}", four times. So it is correct, because array has 4 elements, but why it is empty object? I'm using org.json. Thanks
Jon Skeet
people
quotationmark

You're calling the JSONObject(Object) constructor, passing in a JSONObject (the element in the array). That constructor is documented as: Construct a JSONObject from an Object using bean getters. It reflects on all of the public... more 5/11/2017 6:01:36 AM

people

Converting String to UTF 8 byte array returns a negative value in Java

Let's say I have a byte array and I try to encode it to UTF_8 using the following String tekst = new String(result2, StandardCharsets.UTF_8); System.out.println(tekst); //where...
Jon Skeet
people
quotationmark

Answering the specific question Does that mean that my byte still contains negative values despite converting it to UTF-8? Yes, absolutely. That's because byte is signed in Java. A byte value of -61 would be 195 as an unsigned value.... more 5/10/2017 8:34:29 AM

people

Why does thread.join freezes the UI in vb.net

I am trying my hand on multi threading. After creating the threads my UI seems to freeze as i am trying to join the created threads. If i dont join my threads then everything...
Jon Skeet
people
quotationmark

You're calling Thread.Join from the UI thread. That means the UI thread is blocked until all the other threads complete. That's a bad idea in general as it means your UI is blocked while all the other threads do work. In this case it's... more 5/10/2017 7:14:39 AM

people

null shorthand in C#?

Is there a way to make this line shorter? bool pass = d != null && d["k"] != null && (bool)d["k"]; Note: "k" is actually "a longer id"; I replaced it to make it...
Jon Skeet
people
quotationmark

A combination of the null-conditional and null-coalescing operators should work here: bool pass = (bool) (d?["k"] ?? false); To clarify: If d is null, d?["k"] is null If d?["k"] is null, d?["k"] ?? false is false (boxed as object) The... more 5/9/2017 7:02:45 AM

people

Unix timestamp function not returning expected timestamp, wrong timezone?

I'm using a custom function to take the current date and subtract days on it then return a timestamp in unix format. All the conversions are working fine except the expected...
Jon Skeet
people
quotationmark

You're calling the DateTime constructor without specifying a Kind, then you're calling ConvertTimeToUtc. That's going to assume that dateTime is actually local, and convert it to UTC. Given that you're already constructing it with UTC... more 5/8/2017 6:01:15 PM

people

Specific Elements from an XML document

I have the following XML: <?xml version="1.0" encoding="utf-8" ?> <catalog> <books> <book id="bk101"> <author id="1">Gambardella,...
Jon Skeet
people
quotationmark

Currently you're trying to fetch a price element directly under book, so that's not going to work to start with. Next you want to only select the price element with the right country sub-element Finally, you don't want the value of the... more 5/8/2017 8:51:02 AM

people

gradle javadoc fail on UTF 8 chars, only on windows

So i have a gradle based java project, with tons of hungarian docs string, witch mean a lot of non ASCII character. It's perfectly fine since javac consume utf-8 characters in...
Jon Skeet
people
quotationmark

I suspect you want options.encoding = 'UTF-8' That would fit with the javadoc help text of: -encoding <name> Source file encoding name ... whereas charSet appears to affect the output. (I'd hope it would default... more 5/8/2017 8:37:35 AM

people

Why is an async method with await keyword still blocking the main thread?

Can anyone explain to me the difference between these 2 async methods? Method A public async Task<List<Thumbnail>> GetAllThumbnailsAsync() { return await...
Jon Skeet
people
quotationmark

Method A is basically executing everything in a separate task, which will probably end up in a new thread. When you await the resulting task, you won't block anything. Method B starts by calling GetAllDirectoriesWithImageAsync, and awaits... more 5/8/2017 7:02:11 AM

people