Need help with Java code - python dictionary into java hashmap

0 Paul D · September 6, 2014
Hello,

I am using python code on a raspberry pi to save some settings.  I am using a python dictionary (shown in the code below)  "Mode" is key1 and "Standalone is value1.

I am also running a tomcat7 apache web server on the raspberry and I am using .jsp Java Server Page to create a nice gui or user interface.

I am able to open the file with in a JSP but I what I would like to do is turn the python dictionary into a java hashmap.

python code:
import json

mydict = {'Mode':'Standalone','Email':'No','Type':'Unknown'} #my dictionary

filename = 'C:\dict.txt' #file location

def WriteFile():
    try:
        with open(filename, 'wb') as fp:  # writing the file
            json.dump(mydict, fp)
    except:
        print 'Error in WriteFile'

def ReadFile():
    try:
        with open(filename, 'rb') as fp:  # read the file
            mydict = json.load(fp)
    except:
        print 'Error in ReadFile'


java code for my Java Server Page

<%@page import="java.io.File"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.net.URL"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>

                <%
                String txtFilePath = "C:\\dict.txt";
                BufferedReader reader = new BufferedReader(new FileReader(txtFilePath));
                StringBuilder sb = new StringBuilder();
                String line;

                while((line = reader.readLine())!= null){
                    sb.append(line+"\n");
                }
                out.println(sb.toString());
                reader.close();
                %>



I can get my dictionary to output with no problems but I am having trouble converting the string builder object into a java hashmap.

Any help or suggestions would be greatly appreciated.

Post a Reply

Replies

Oldest  Newest  Rating
+1 Paul D · September 7, 2014
I came up with this solution and wanted to share.  I know it is not the best code but I will clean it up and improve it.
The code does function and I can turn a python dictionary into a java hashmap and then back into a python dictionary.

package str;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class a {

    public static void main(String[] args) throws IOException {

        HashMap<String, String> map = new HashMap<String, String>();
        String txtFilePath = "C:\\dict.txt";
        BufferedReader reader = new BufferedReader(new FileReader (new File(txtFilePath)));
        StringBuilder sb = new StringBuilder();
        String line;
        while((line = reader.readLine())!= null){
            sb.append(line+"\n");
            String str1 = sb.toString().replace("{", " ");
            String str2 = str1.replace("}", " ");
            String str3 = str2.replace("\"", " ");
            if (line.contains(":")) {
                String[] strings = line.split("");
                String[] subString=str3.split(",");
                for(String i:subString){
                    if(i.contains(":")){
                        map.put(i.split(":")[0], i.split(":")[1]);   
                    }
                }
            }
            System.out.println ("text is now a Java Hashmap " + (map.toString()));
            
System.out.println ("Back to a Python Dictionary " + (map.toString().replace("=", ":")));
        }
    }
}
  • 1

Java / Android Development

107,033 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator