This is My String
{Line:1,Direction:incoming,LocalUsername:xxx,AuthUsername:31223,PeerUsername:04232000113,Name:04232000113,Server:23424,Connectime:2189msec,Duration:0msec,DiscBy:Remote,Reason:cancelNormalcallclearing}
i want to convert it to json string
{"Line":"1","Direction":"incoming","LocalUsername":"xxx","AuthUsername":"31223","PeerUsername":"04232000113","Name":"04232000113","Server":"23424","Connectime":"2189msec","Duration":"0msec","DiscBy":"Remote"}
Since it isn't nested, simply add " around : and ,, and after { and before }:
s = s.replaceAll("(?<=[{:,])|(?=[:,}])", "\"");
See demo on regex101.com.
Try using Gson library
Gson g = new Gson();
Player p = g.fromJson(jsonString, Player.class)
You can also convert a Java object to JSON by using toJson() method as shown below
String str = g.toJson(p);
If you don't have a POJO representing the data, it can be done generally using JsonElement.
Demo
String input = "{Line:1,Direction:incoming,LocalUsername:xxx,AuthUsername:31223,PeerUsername:04232000113,Name:04232000113,Server:23424,Connectime:2189msec,Duration:0msec,DiscBy:Remote,Reason:cancelNormalcallclearing}";
Gson g = new Gson();
JsonElement root = g.fromJson(input, JsonElement.class);
String result = g.toJson(root);
System.out.println(result);
Output
{"Line":1,"Direction":"incoming","LocalUsername":"xxx","AuthUsername":31223,"PeerUsername":"04232000113","Name":"04232000113","Server":23424,"Connectime":"2189msec","Duration":"0msec","DiscBy":"Remote","Reason":"cancelNormalcallclearing"}
Related
I have a string that is being returned by an API which looks like this :
String result1 = "{username=null, tokenCode=00000000-0000-0000-0000-0000754d6034}";
I converted this to
String result2 = "{"username":"null", "tokenCode":"00000000-0000-0000-0000-0000754d6034"}";
Below is the code I am using it to convert to result2 from result1.
String result2 = new GsonBuilder().serializeNulls().create().toJson(result1);
Again I am doing this to make it a JSON object from String.
JSONObject jsonObject = new JSONObject(result2);
Is there a better way I can directly convert the "result1" to JSONObject without having to convert it to "result2".
In a Java application with an OrientDB database, after I have a Vertex object, I need to extract its properties in a String object. This object must be in Json format.
An example of expected result is:
{"#type":"d","#rid":"#13:1093","#version":1,"#class":"V_Notification","lastUpdateDate":"2016-07-20 16:45:31","lastUpdateUser":"#12:41","creationDate":"2016-07-20 16:45:31","creationUser":"#12:41","type":"user_added_to_share_made_upload","description":"user_added_to_share_made_upload","sphereId":"#16:18","out_E_NotificationUser":["#45:1091"],"deleted":false,"version":0,"isRead":false,"#fieldTypes":"lastUpdateDate=t,lastUpdateUser=x,creationDate=t,creationUser=x,out_E_NotificationUser=g"}
You could use
OrientVertex v=g.getVertex("#9:0");
ODocument d=v.getRecord();
String json=d.toJSON();
Hope it helps
You can try gson library and than use something like:
Gson gson = new Gson();
String jsonInString = gson.toJson(yourOrientObj);
Ref.: mkyong.com
I made an example to try your case:
#class: V_Notification
Property: description
Vertex v = graph.getVertex("#17:0");
Gson gson = new Gson();
String jsonInString = gson.toJson(v.getProperty("description").toString());
System.out.println("STAMPO = " + jsonInString);
This is my output:
PRINTED = "user_added_to_share_made_upload"
Hope it helps.
Regards.
Here is my json:
{
"timestamp":"04295d4f-2a6f-4a38-a818-52108cbdc358",
"lastFullSyncDate":null,
"ftpInfo":null,
"listingInfo":{
"itemID":"110179365615",
"itemTitle":"test",
"itemPrice":"88.2235294117647",
.......
....
.....
}
}
I have a java class named listingInfo was trying to use gson to convert the string with the key of listingInfo to the class, but i'm getting nulls for all the vars.
Gson gson = new Gson();
gson.fromJson(json, ListingInfo.class);
While trying to convert to the part class which contains the time stamp and etc i dot get the vars but the listingInfo is null inside
Is it possible to get into the nested key and only convert him to the class?
You can do it by parsing whole json tree and then extracting the nested key
String json = ...; //your json string
Gson gson = new Gson();
JsonElement element = new JsonParser().parse(json); //parse to json tree
JsonElement listingElement = element.getAsJsonObject().get("listingInfo"); // extract key
ListingInfo listingInfo = gson.fromJson(listingElement, ListingInfo.class);
I have a Json Array as string without name and I want to parse it how can i do it in android ?
My array :
{"emp_info":[
{"id":"1","groupe":"1","professeur":"1"},
{"id":"2","groupe":"2","professeur":"1"}
]}
This is how you can parse it
Assuming your json string is data
JSONObject jsonObj = new JSONObject(data);
JSONArray empInfo = jsonObj.getJSONArray("emp_info");
for(int i = 0; i < empInfo.length(); i++){
JSONObject obj = empInfo.getJSONObject(i);
String id = obj.getString("id");
String groupe = obj.getString("groupe");
String professeur = obj.getString("professeur");
}
The example json you gave has a name, but if it doesn't this is how I do it. Using Gson to parse JSON, I use TypeToken to tell the gson builder it's an array.
List<MyObject> jsonObject = new Gson().fromJson(json, new TypeToken<List<MyObject>>().getType());
With the following code you'll have an object representation of your json array.
I have the following problem:
I have an ArrayList in Java.
I convert the ArrayList to string like this:
Gson gson = new Gson();
String feeds += gson.toJson(arrayList);
This is the result:
[{"status":"ERROR","position":"[48.2748206,8.849529799999999]"}]
But i need the following output:
[{"status":"ERROR","position": [48.2748206,8.849529799999999]}]
The Value of position should be without quotes. How can i realize that?
Many thanks in advance
Greets
Replace the double quotes around position's value using String#replaceAll() method. Just create a regex and replace double quotes with empty sting.
Try with Positive Lookbehind and Positive Lookahead.
sample code:
String json = "[{\"status\":\"ERROR\",\"position\":\"[48.2748206,8.849529799999999]\"}]";
String regex = "(?<=\"position\":)\"|\"(?=\\}\\])";
System.out.println(json.replaceAll(regex, ""));
Here is DEMO
Try with grouping and substitutions as well.
sample code:
String json = "[{\"status\":\"ERROR\",\"position\":\"[48.2748206,8.849529799999999]\"}]";
String regex = "(\"position\":)\"([^\"]*)\"";
System.out.println(json.replaceAll(regex, "$1$2"));
Here is DEMO
I don't think you should go like this, may be you should change your work structure, But if you do want to typecast manually, then you can do it this way.
Suppose you have a JSONArray object like this:
JSONArray arr=[{"status":"ERROR","position":"[48.2748206,8.849529799999999]"}];
Then you can take out JSONObject like this:
Iterator iterator = array.iterator();
while(iterator.hasNext()){
Gson gson = new Gson();
ClassToCastInto obj = gson.fromJson((JsonElement)iterator.next();, ClassToCastInto.class);
System.out.println(obj.someProperty);
}
Consider using a Gson JsonWriter:
StringWriter buffer = new StringWriter();
JsonWriter writer = new JsonWriter(buffer);
writer.beginArray().beginObject();
writer.name("status").value("ERROR");
writer.name("position").beginArray();
for (double value : Arrays.asList(48.2748206, 8.849529799999999)) {
writer.value(value);
}
writer.endArray().endObject().endArray().close();
String json = buffer.toString();