Java Dynamic JSON data parsing
You may wonder what it isDynamic json dataIn fact, I am not very sure about this name. Let's call it for the time being. When I developed a Law Enforcement recorder (the background does not know how to implement it ), when obtaining the video image information on the recorder,UniqueJson data in this format
[ { file1: date1 }, { file2: date2 }, { file3: date3 }, { file4: date4 }]
It looks like normal json data, but after careful consideration, the key and value are all changed. The key is the file name, and the value is the time. I have no idea at all, what json parsing framework does not work, what is returned, how can I get the data ......
I searched a lot on the Internet, but I don't know how to search for it. I am so worried that I want to calm down and analyze it. First of all, the format I need is as follows:
[ { file:file1, date:date1 }, { file:file2, date:date2 }, { file:file3, date:date3 }, { file:file4, date:date4 }]
If this is the case, we can proceed with this thinking. The focus of the question is how to convert the awkward json format into the desired json format, json data is essentially a normal String, so check whether some functions of String can be used for processing. The correct idea is that a replace method is found to test the code.
public class Test { public static void main(String[] args) { String data = [ + { + ile1: date1\n + }, + { + ile2: date2\n + }, + { + ile3: date3\n + }, + { + ile4: date4\n + } + ]; data = data.replace({ + ,{ + ile:); data = data.replace(: ,, + date:); System.out.println(data = + data); }}
Print results
data = [ { file:file1, date:date1 }, { file:file2, date:date2 }, { file:file3, date:date3 }, { file:file4, date:date4 }]Process finished with exit code 0
Haha, thanks to the conversion of the two lines of code, you can finally have fun,Sometimes, the problem is not that difficult