Java Dynamic JSON data parsing

Source: Internet
Author: User
Tags date1

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.