1

I am making a JSON request to server using Java. Here is the following parameters.

{method:'SearchBySearchConfiguration',params:[{{SearchCriteria:'%arriva',
 IsAccountSearch:true,IsContactSearch:false,SearchByName:true,SearchByAddress:false
 CRMTextValues:[], CRMCurrencyValues:[]}]}

I could do this way.

 JSONObject json=new JSONObject();
 json.put("method", "SearchBySearchConfiguration"); 

How do I add the rest of params, in name-value pair to JSON object?

Thanks in advance!

asked Sep 21, 2012 at 11:08

6 Answers 6

2

One way I can think of is using the org.json library. I wrote a sample to build part of your request object:

public static void main(String[] args) throws JSONException {
 JSONObject jsonObject = new JSONObject();
 jsonObject.put("method", "SearchBySearchConfiguration");
 JSONArray jsonArray = new JSONArray();
 JSONObject innerRecord = new JSONObject();
 innerRecord.put("SearchCriteria", "%arriva");
 innerRecord.put("IsAccountSearch", true);
 jsonArray.put(innerRecord);
 jsonObject.put("params",jsonArray);
 System.out.println("jsonObject :"+jsonObject);
}

The output is :

jsonObject :{"method":"SearchBySearchConfiguration","params":[{"IsAccountSearch":true,"SearchCriteria":"%arriva"}]}

Another technique would be to build Java objects that resemble your request structure. You can then convert it into json using Jackson library's ObjectMapper class.

In both cases once you get the json string, you can directly write it into the request body.

answered Sep 21, 2012 at 11:32
Sign up to request clarification or add additional context in comments.

Comments

1
JSONObject json=new JSONObject();
 json.put("method", "SearchBySearchConfiguration"); 
 JSONArray paramsArr = new JSONArray();
 JSONObject arrobj = new JSONOject();
 arrobj.put("SearchCriteria","%arriva");
 arrobj.put("IsAccountSearch","true");
 arrobj.put("IsContactSearch","false");
 arrobj.put("SearchByName","true");
 arrobj.put("SearchByAddress","false");
 arrobj.put("CRMTextValues",new JSONArray());
 arrobj.put("CRMCurrencyValues",new JSONArray());
 paramsArr.put(arrobj);
 json.put("params",paramsArr);
answered Sep 21, 2012 at 11:21

Comments

0

The you can create JSONArray and put that array in JSONObject

answered Sep 21, 2012 at 11:12

Comments

0

Its Better to use gson for this.

First you need to create classs with following members :

public class TestClass{
 private String method;
 private ParamClass params;
 }
public class ParamClass{
 private String SearchCriteria;
 private boolean IsAccountSearch;
 private boolean IsContactSearch;
 private boolean SearchByName;
 private boolean SearchByAddress;
 private String[] CRMTextValues;
 private String[] CRMCurrencyValues;
}

Usage :

Serializing :

Gson gson = new Gson(); 
String jsonString = gson.toJson(testClassObject);

Deserializing :

Gson gson = new Gson();
TestClass testClassObject = gson.fromJson(jsonString , TestClass.class);
answered Sep 21, 2012 at 11:21

Comments

0

See this below example, where a JSONArray is returned and then how i am converting it in JSONObject form...

public JSONArray go() throws IOException, JSONException {
JSONArray json = readJsonFromUrl("http://www.xxxxxxxx.com/AppData.aspx");
return json;
 }
JSONArray jarr;
for(int i=0 ; i<jarr.length() ; i++){
JSONObject jobj = jarr.getJSONObject(i);
String mainText = new String();
String provText = new String();
String couText = new String(); 
try{
mainText = jobj.getString("Overview");
System.out.println(mainText);
}catch(Exception ex){}
try{
JSONObject jProv = jobj.getJSONObject("Provider");
provText = jProv.getString("Name");
System.out.println(provText);
}catch(Exception ex){}
try{ 
JSONObject jCou = jobj.getJSONObject("Counterparty");
couText = jCou.getString("Value");
System.out.println(couText);
}catch(Exception ex){}

Jackson is a very efficient to do JSON Parsing

See this link:

http://jackson.codehaus.org/

Gson is provided by google which is also a good way to handle JSON.

answered Sep 21, 2012 at 11:22

Comments

0

To add params, JSONArray is used. Inside params, we use JSONObject to add data such as SearchByAddress, IsAccountSearch ..etc. Reference http://www.mkyong.com/java/json-simple-example-read-and-write-json/

package com.test.json;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class JsonSimpleExample {
 public static void main(String[] args) {
 JSONObject obj = new JSONObject();
 obj.put("method", "SearchBySearchConfiguration");
 JSONArray list = new JSONArray();
 JSONObject innerObj = new JSONObject();
 innerObj.put("SearchCriteria","%arriva" );
 innerObj.put("IsAccountSearch",true);
 innerObj.put("IsContactSearch",false);
 innerObj.put("SearchByName",true);
 innerObj.put("SearchByAddress",false);
 innerObj.put("CRMTextValues",new JSONArray());
 innerObj.put("CRMCurrencyValues",new JSONArray());
 list.add(innerObj);
 obj.put("params", list);
 System.out.print(obj);
 }
}
answered Sep 21, 2012 at 11:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.