0

I want to convert the stdout of the following python into a json object in Java code.

Process p = Runtime.getRuntime().exec(python one.py);

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

while ((s = stdInput.readLine()) != null) {
    System.out.println(s); }

python output sample: cat-red-fish dog-brown-meat

desired JSON : "animal":{"type":"cat", "color":"red", "food":"fish"}...

How can I split while passing the output in JSON?

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
ironist
  • 11
  • 1
  • 2
  • Split each line using `String[] arr = String.split("-")` and then with result array parts simply concatenate the strings. `String json = "\"animal\":{\"type\":\"" + arr[0] + "\" ...`. – Edd Jul 05 '17 at 19:20
  • 1
    Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – zero298 Jul 05 '17 at 19:36

2 Answers2

1

If you're okay with using libraries, you can use something like Google's Gson to map the string to an object.

https://github.com/google/gson

With this you can create three String/Enum fields inside an Animal class like:

public class Animal {
  private String type;
  private String color;
  private String food;
}

You can then use Gson#fromJson("jsonString", Animal.class);

Amir Omidi
  • 329
  • 1
  • 11
0

Use JacksonJSON library or GSON which may be easiest one. http://www.studytrails.com/java/json/java-google-json-parse-json-to-java-tree/

See this answer how to create JSON object from Map<String,String> object. You just need to split an input string accordingly. https://stackoverflow.com/a/18444414/185565

Map<String,String> myMap = new HashMap<String,String>();
//..populate mymap..
Gson gson = new Gson(); 
String json = gson.toJson(myMap); 

Here is a copypaste example from the gson documentation in case the previous link stopped working. This one reads a json string to json object so is not a direct answer to your question but may come handy.

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;     
import org.apache.commons.io.IOUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class ParseTreeExample6 {
    public static void main(String[] args) throws MalformedURLException, IOException {
        String url = "http://freemusicarchive.org/api/get/albums.json?api_key=60BLHNQCAOUFPIBZ&limit=5";
        String json = IOUtils.toString(new URL(url));
        JsonParser parser = new JsonParser();
        JsonElement element = parser.parse(json);
        if (element.isJsonObject()) {
            JsonObject albums = element.getAsJsonObject();
            System.out.println(albums.get("title").getAsString());
            JsonArray datasets = albums.getAsJsonArray("dataset");
            for (int i = 0; i < datasets.size(); i++) {
                JsonObject dataset = datasets.get(i).getAsJsonObject();
                System.out.println(dataset.get("album_title").getAsString());
            }
        }

    }
}
Whome
  • 10,181
  • 6
  • 53
  • 65