Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

two-dimensional string array to JSON #54

Closed
GiantCrocodile opened this issue May 17, 2017 · 8 comments
Closed

two-dimensional string array to JSON #54

GiantCrocodile opened this issue May 17, 2017 · 8 comments

Comments

@GiantCrocodile
Copy link

GiantCrocodile commented May 17, 2017

I tried to convert a two-dimensional string array to a JSON string. How can I do this? I tried to use JsonStream.serialize() with my string:

String content = {
    {"key1", "value1"},
    {"key2", "value2"}
};

I want a JSON with both keys on same level - not as a JSON array. Jsoniter is just parsing the String and gives back an object with same structure as input - not JSON format. Please point me to the right direction or where I should look at the docs. I tried it for one hour and got no real use results.

@taowen
Copy link
Contributor

taowen commented May 17, 2017

can you give a sample code, with expected result?

@GiantCrocodile
Copy link
Author

GiantCrocodile commented May 17, 2017

Hey @taowen! I want this JSON string to be crafted/produced by your JSON library without putting JSON formatting around the values and key names myself:

"{\"type\": \"ANT_ACTION_GOLEFT\", \"color\": \"ANT_COLOR_BLUE\"}" 

So I want a normal String object to represent my key type with value ANT_ACTION_GOLEFT and key color with ANT_COLOR_BLUE and get it converted to JSON by the library.

@taowen
Copy link
Contributor

taowen commented May 18, 2017

"{\"type\": \"ANT_ACTION_GOLEFT\", \"color\": \"ANT_COLOR_BLUE\"}"

this is the input or output? If this is the output, what is the java code to define to object or map in the first place? Do you have a working piece of code?

@GiantCrocodile
Copy link
Author

GiantCrocodile commented May 18, 2017

The quoted string is how it should look like (without the escape symbols of course). I want this output:

{
"type": "ANT_ACTION_GOLEFT",
"color": "ANT_COLOR_BLUE"
}

but I don't know what kind of string or array object I should pass to serializer so I get this output. I would expect a 2D String array to work but not sure.

@taowen
Copy link
Contributor

taowen commented May 18, 2017

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("type", "ANT_ACTION_GOLEFT");
map.put("color", "ANT_COLOR_BLUE");
JsonStream.serialize(map);

@GiantCrocodile
Copy link
Author

This works @taowen! Although it feels like it is a bit too complex for just two two keys with simple String values. Is there something where I can read up on which types are supported and how to put them into serializer, like to see if there are alternatives to HashMap for my case?

@taowen
Copy link
Contributor

taowen commented May 18, 2017

this is the simplest way...

@GiantCrocodile
Copy link
Author

Okay, fair enough. Thank you :)! The docs were a bit hard to understand for me as a newbie to this lib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants